Android开发学习教程(14)- Android布局之相对布局RelativeLayout

244 阅读3分钟

—— 珍贵的东西往往数目稀少,所以你要变得更强,才有力量去抢。

上一篇我们讲了线性布局LinearLayout的基本用法,这里来学习常用布局之相对布局RelativeLayout的基本用法。

相对布局是什么

相对布局按照控件间的相对位置排列,比如控件A相对于控件B在控件B的左边,并且和控件B顶部对齐等。

相对布局有什么用

通过相对位置来控制控件在屏幕的位置。

相对布局怎么用

继续基于上一篇的项目,我们新建一个RelativeLayoutActivity:

12345678910111213141516171819202122232425262728293031323334353637383940414243<?``xml version``=``"1.0" encoding``=``"utf-8"``?>``<``RelativeLayout xmlns:android``=``"http://schemas.android.com/apk/res/android"``    ``android:layout_width``=``"match_parent"``    ``android:layout_height``=``"match_parent"``>``    ``<``TextView``        ``android:id``=``"@+id/tv1"``        ``android:layout_width``=``"wrap_content"``        ``android:layout_height``=``"100dp"``        ``android:layout_centerInParent``=``"true"``        ``android:background``=``"#F0F0F0"``        ``android:gravity``=``"center"``        ``android:text``=``"我是第一个子控件" />``    ``<``TextView``        ``android:layout_width``=``"wrap_content"``        ``android:layout_height``=``"wrap_content"``        ``android:layout_alignTop``=``"@+id/tv1"``        ``android:layout_toLeftOf``=``"@+id/tv1"``        ``android:text``=``"我是第二个子控件" />``    ``<``TextView``        ``android:layout_width``=``"wrap_content"``        ``android:layout_height``=``"wrap_content"``        ``android:layout_alignTop``=``"@+id/tv1"``        ``android:layout_toRightOf``=``"@+id/tv1"``        ``android:text``=``"我是第三个子控件" />``    ``<``TextView``        ``android:layout_width``=``"wrap_content"``        ``android:layout_height``=``"wrap_content"``        ``android:layout_alignBottom``=``"@+id/tv1"``        ``android:layout_toLeftOf``=``"@+id/tv1"``        ``android:text``=``"我是第四个子控件" />``    ``<``TextView``        ``android:layout_width``=``"wrap_content"``        ``android:layout_height``=``"wrap_content"``        ``android:layout_alignBottom``=``"@+id/tv1"``        ``android:layout_toRightOf``=``"@+id/tv1"``        ``android:text``=``"我是第五个子控件" />``</``RelativeLayout``>

Android开发学习教程(14)- Android布局之相对布局RelativeLayout

第一个控件

12345678<``TextView``    ``android:id``=``"@+id/tv1"``    ``android:layout_width``=``"wrap_content"``    ``android:layout_height``=``"100dp"``    ``android:layout_centerInParent``=``"true"``    ``android:background``=``"#F0F0F0"``    ``android:gravity``=``"center"``    ``android:text``=``"我是第一个子控件" />
123android:layout_height="100dp":表示TextView控件高度为100dp``android:layout_centerInParent="true":表示TextView控件位于父控件RelativeLayout的水平居中并且垂直居中``android:gravity="center":表示TextView控件上面显示的内容水平居中并且垂直居中

第二个控件

123456<``TextView``    ``android:layout_width``=``"wrap_content"``    ``android:layout_height``=``"wrap_content"``    ``android:layout_alignTop``=``"@+id/tv1"``    ``android:layout_toLeftOf``=``"@+id/tv1"``    ``android:text``=``"我是第二个子控件" />
12android:layout_alignTop="@+id/tv1":表示与id为tv1控件顶部对齐``android:layout_toLeftOf="@+id/tv1":表示在id为tv1控件的左边

第三个控件

123456<``TextView``    ``android:layout_width``=``"wrap_content"``    ``android:layout_height``=``"wrap_content"``    ``android:layout_alignTop``=``"@+id/tv1"``    ``android:layout_toRightOf``=``"@+id/tv1"``    ``android:text``=``"我是第三个子控件" />
12android:layout_alignTop ="@+id/tv1"表示与id为tv1控件顶部对齐``android:layout_toRightOf ="@+id/tv1"表示在id为tv1控件的右边

第四个控件

123456<``TextView``    ``android:layout_width``=``"wrap_content"``    ``android:layout_height``=``"wrap_content"``    ``android:layout_alignBottom``=``"@+id/tv1"``    ``android:layout_toLeftOf``=``"@+id/tv1"``    ``android:text``=``"我是第四个子控件" />
12android:layout_alignBottom="@+id/tv1"表示与id为tv1控件底部对齐``android:layout_toLeftOf="@+id/tv1"表示在id为tv1控件的左边

第五个控件

123456<``TextView``    ``android:layout_width``=``"wrap_content"``    ``android:layout_height``=``"wrap_content"``    ``android:layout_alignBottom``=``"@+id/tv1"``    ``android:layout_toRightOf``=``"@+id/tv1"``    ``android:text``=``"我是第五个子控件" />
12android:layout_alignBottom="@+id/tv1"表示与id为tv1控件底部对齐``android:layout_toRightOf="@+id/tv1"表示在id为tv1控件的右边

RelativeLayout常用基本属性

第一类属性 属性值为true或者false

12345678910111213android:layout_centerHrizontal:水平居中``android:layout_centerVertical:垂直居中``android:layout_centerInparent:相对于父控件完全居中``android:layout_alignParentBottom:贴紧父控件的下边缘``android:layout_alignParentLeft:贴紧父控件的左边缘``android:layout_alignParentRight:贴紧父控件的右边缘``android:layout_alignParentTop:贴紧父控件的上边缘

第二类属性 属性值必须为id的引用名“@id/id-name”

123456789101112131415android:layout_below:在某控件下方``android:layout_above:在某控件上方``android:layout_toLeftOf:在某控件的左边``android:layout_toRightOf:在某控件的右边``android:layout_alignTop:本控件的上边缘和某控件的上边缘对齐``android:layout_alignLeft:本控件的左边缘和某控件的左边缘对齐``android:layout_alignBottom:本控件的下边缘和某控件的下控件对齐``android:layout_alignRight:本控件的右边缘和某控件的有边缘对齐