Android之RelativeLayout相对布局

什么是RelativeLayout

RelativeLayout顾名思义,相对布局,在这个容器内部的子元素们可以使用彼此之间的相对位置或者和容器间的相对位置来进行定位。

注意:不能在RelativeLayout容器本身和他的子元素之间产生循环依赖。


与RelativeLayout相关的布局属性
android:layout_above 将该控件置于给定ID的控件之上
android:layout_below 将该控件的置于给定ID控件之下
android:layout_toLeftOf 将该控件置于给定ID的控件之左
android:layout_toRightOf 将该控件置于给定ID的控件之右

android:layout_alignBaseline 该控件基线对齐给定ID的基线
android:layout_alignBottom 该控件于给定ID的控件底部对齐
android:layout_alignLeft 该控件于给定ID的控件左对齐
android:layout_alignRight 该控件于给定ID的控件右对齐
android:layout_alignTop 该控件于给定ID的控件顶对齐

android:layout_alignParentLeft 如果为True,该控件位于父控件的左部
android:layout_alignParentRight 如果为True,该控件位于父控件的右部
android:layout_alignParentTop 如果为True,该控件位于父控件的顶部
android:layout_alignParentBottom 如果为True,该控件位于父控件的底部

android:layout_centerHorizontal 如果为True,该控件将被置于水平方向的中央
android:layout_centerInParent  如为Ture,该控件将被置于父控件水平方向和垂直方向
android:layout_centerVertical 如果为True,该控件将被置于垂直方向的中央


下面来实现这样一个简单的效果


先来分析一下这个布局

首先是一个test1

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test1"
        android:background="#FF0000" />

然后,test2在test1的右边

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test22"
        android:background="#00FF00"
        android:layout_toRightOf="@id/tv1" />

test3在test1的下边,并且和test2右对齐

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test333"
        android:background="#0000FF"
        android:layout_below="@id/tv1"
        android:layout_alignRight="@id/tv2" />

test4在父框架的底部

    <TextView
        android:id="@+id/tv4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test4444"
        android:background="#FFFF00"
        android:layout_alignParentBottom="true" />

test5在父框架的水平垂直中央

    <TextView
        android:id="@+id/tv5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test55555"
        android:background="#FF00FF"
        android:layout_centerInParent="true" />

test6在父框架的底部右边

    <TextView
        android:id="@+id/tv6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test666666"
        android:background="#00FFFF"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true" />

好了,关于Android之RelativeLayout相对布局就简单尝试到这里了,以后我们在开发布局过程中可以根据需要选择设置适当的属性,谢谢!


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值