Android中RelativeLayout详解


android:layout_above="@id/xxx"  --将控件置于给定ID控件之上
android:layout_below="@id/xxx"  --将控件置于给定ID控件之下

android:layout_toLeftOf="@id/xxx"  --将控件置于给定ID控件之左
android:layout_toRightOf="@id/xxx"  --将控件置于给定ID控件之右



android:layout_alignLeft="@id/xxx"  --将控件的左边缘和给定ID控件的左边缘对齐
android:layout_alignTop="@id/xxx"  --将控件的上边缘和给定ID控件的上边缘对齐
android:layout_alignRight="@id/xxx"  --将控件的右边缘和给定ID控件的右边缘对齐
android:layout_alignBottom="@id/xxx"  --将控件的底边缘和给定ID控件的底边缘对齐

这几个是配合其他的几个使用的

<TextView
        android:id="@+id/textView0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView0"
        android:layout_alignRight="@id/textView0"
        android:text="abc" />


android:layout_alignParentLeft="true"  --将控件的左边缘和父控件的左边缘对齐
android:layout_alignParentTop="true"  --将控件的上边缘和父控件的上边缘对齐
android:layout_alignParentRight="true"  --将控件的右边缘和父控件的右边缘对齐
android:layout_alignParentBottom="true" --将控件的底边缘和父控件的底边缘对齐
android:layout_centerInParent="true"  --将控件置于父控件的中心位置
android:layout_centerHorizontal="true"  --将控件置于水平方向的中心位置
android:layout_centerVertical="true"  --将控件置于垂直方向的中心位置


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/text1"
        android:layout_toRightOf="@id/text1"
        android:text="align Top to text1"
        android:textSize="40px" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/text2"
        android:layout_below="@id/text2"
        android:text="align Left to text2"
        android:textSize="30px" />

    <TextView
        android:id="@+id/text4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@id/text3"
        android:layout_toRightOf="@id/text3"
        android:text="abcfgp"
        android:textSize="40px" />

</RelativeLayout>





<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/text1"
        android:layout_toRightOf="@id/text1"
        android:text="align Top to text1"
        android:textSize="40px" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/text2"
        android:layout_below="@id/text2"
        android:text="aaaaa"
        android:background="#00FFFF"
        android:textSize="30px" />

    <TextView
        android:id="@+id/text4"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_alignBottom="@id/text3"
        android:layout_toRightOf="@id/text3"
        android:background="#FFFF00"
        android:text="gggg"
        android:textSize="30px" />

    <TextView
        android:id="@+id/text5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/text3"
        android:layout_below="@id/text3"
        android:text="hello"
        android:background="#E000F0"
        android:textSize="30px" />

    <TextView
        android:id="@+id/text6"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_alignBaseline="@id/text5"
        android:layout_toRightOf="@id/text5"
        android:text="hello rigth"
        android:background="#FF0F00"
        android:textSize="30px" />

</RelativeLayout>




<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="btn4" />

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="btn5" />
    </RelativeLayout>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值