android 自定义相对布局,Android五大布局详解——RelativeLayout(相对布局)

RelativeLayout

接着上一篇,本篇我将介绍RelativeLayout(相对布局)的一些知识点。

RelativeLayout

这是一个非常常用的布局,相比于上节所学到的LinearLayout布局,它更加的随意,可以通过相对定位的方式让控件出现在布局的任何位置。新建UILayoutTestTwo工程,修改activity_main.xml中的代码:

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/button_one"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:text="button 1"/>

android:id="@+id/button_two"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_alignParentTop="true"

android:text="button 2"/>

android:id="@+id/button_three"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="button 3"/>

android:id="@+id/button_four"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentBottom="true"

android:text="button 4"/>

android:id="@+id/button_five"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_alignParentBottom="true"

android:text="button 5"/>

运行程序,效果如下图:

4a938e01713e4ad82166d2a9314aba28.png 以上代码不做过多解释。上面的控件定位是依靠父布局的,其实RelativeLayout中还可以依靠控件进行定位。修改activity_main.xml中的代码:

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/button_three"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="button 3"/>

android:id="@+id/button_one"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_above="@+id/button_three"

android:layout_toLeftOf="@+id/button_three"

android:text="button 1"/>

android:id="@+id/button_two"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_above="@+id/button_three"

android:layout_toRightOf="@+id/button_three"

android:text="button 2"/>

android:id="@+id/button_four"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/button_three"

android:layout_toLeftOf="@+id/button_three"

android:text="button 4"/>

android:id="@+id/button_five"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/button_three"

android:layout_toRightOf="@+id/button_three"

android:text="button 5"/>

运行程序,效果如图所示:

b69448d3e5253df784bcd70f108e2018.png RelativeLayout的属性除了上面提到的,还有很多其他的,以后的学习中遇到了,可以多积累。下面是一些属性的简要说明:

86dedfb124acfb9788f2d503b62de0cf.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值