android RelativeLayout属性和使用

 

android RelativeLayout属性和使用, 实现上面view叠加在下面view之上的效果

Android RelativeLayout 属性
// 相对于给定ID控件

android:layout_above 将该控件的底部置于给定ID的控件之上;

android:layout_below 将该控件的底部置于给定ID的控件之下;

android:layout_toLeftOf    将该控件的右边缘与给定ID的控件左边缘对齐;

android:layout_toRightOf  将该控件的左边缘与给定ID的控件右边缘对齐;

android:layout_alignBaseline  将该控件的baseline与给定ID的baseline对齐;

android:layout_alignTop        将该控件的顶部边缘与给定ID的顶部边缘对齐;

android:layout_alignBottom   将该控件的底部边缘与给定ID的底部边缘对齐;

android:layout_alignLeft        将该控件的左边缘与给定ID的左边缘对齐;

android:layout_alignRight      将该控件的右边缘与给定ID的右边缘对齐;

// 相对于父组件

android:layout_alignParentTop      如果为true,将该控件的顶部与其父控件的顶部对齐;

android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐;

android:layout_alignParentLeft      如果为true,将该控件的左部与其父控件的左部对齐;

android:layout_alignParentRight    如果为true,将该控件的右部与其父控件的右部对齐;

// 居中

android:layout_centerHorizontal 如果为true,将该控件的置于水平居中;

android:layout_centerVertical     如果为true,将该控件的置于垂直居中;

android:layout_centerInParent   如果为true,将该控件的置于父控件的中央;

// 指定移动像素

android:layout_marginTop      上偏移的值;

android:layout_marginBottom 下偏移的值;

android:layout_marginLeft   左偏移的值;

android:layout_marginRight   右偏移的值;


example:1.

android:layout_below = "@id/***"

android:layout_alignBaseline = "@id/***"

android:layout_alignParentTop = true

android:layout_marginLeft = “10px”

2. 使用RelativeLayout实现叠加的效果上面的view覆盖下面的view。

对任何布局,通过android:layout_marginXXX等属性设置为负值来实现相邻view之间的叠加效果,

例如:

android:layout_marginTop="-50dip" 可实现与相邻的顶端view叠加50dip区域的效果。

但是,谁叠加在谁的上面是由他们在xml文件中的描述顺序决定的, 后出现的会在上面。
因此如果要让布局上面的view覆盖在下面的view之上,不能使用LinearLayout。因为, 对于LinearLayout,在xml文件中的描述view时,必须先描述上方的view,让后描述下方的view,所以下方view的会覆盖在上方view之上。
可以使用RelativeLayout。因为在Relativelayout的布局中,我们可以先描述处于下方的view,后描述上方的view。(注意, 此时先描述的View需要使用后描述的view的ID, 需要使用"@+id/相应的viewID")
例如:下面就是蓝色的button会覆盖绿色button之上的例子

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:orientation="vertical">
      <Button 

              android:layout_below="@+id/bt1"

              android:layout_above="@+id/bt2"
             android:layout_width="fill_parent"

             android:layout_height="fill_parent"

             android:background="#ff00ff00"

             android:layout_marginTop="-50dip"/>

             <Button android:id="@+id/bt1"

                        android:layout_width="fill_parent"

                        android:layout_height="100dip"

                        android:background="#ff0000ff"/>
             <Button android:id="@+id/bt2"

                       android:layout_alignParentBottom="true"

                       android:layout_width="fill_parent"

                       android:layout_height="100dip"

                       android:background="#ff0000ff"/>
</RelativeLayout>

 


 

 

example:2.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent" android:layout_height="fill_parent" 
                android:background="#000000">

 <ImageView  android:id="@+id/channellogo"         

            android:layout_height="wrap_content"

            android:layout_width="wrap_content" 
            android:src="@drawable/icon"/>

 <TextView android:id="@+id/startime"
              android:layout_width="wrap_content" 

              android:textSize="16dp" 
              android:layout_height="wrap_content"

              android:padding="3dp"
              android:scrollbars="vertical" 
              android:textColorHighlight="#ff0000ff"
              android:text="title" 

             android:layout_toRightOf="@+id/channellogo" />
 <TextView android:id="@+id/program"
             android:layout_width="wrap_content"

             android:textSize="16dp" 
             android:layout_height="wrap_content" 

            android:padding="3dp"
            android:scrollbars="vertical"
            android:textColorHighlight="#ffff0000"

           android:layout_toRightOf="@+id/channellogo"
           android:text="description"

           android:layout_below="@+id/startime"/>
</RelativeLayout> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值