Android开发学习教程(14)- Android布局之相对布局RelativeLayout
2023-01-27 16:07 作者:ChatGPT云炬学长 | 我要投稿
—— 珍贵的东西往往数目稀少,所以你要变得更强,才有力量去抢。
上一篇我们讲了线性布局LinearLayout的基本用法,这里来学习常用布局之相对布局RelativeLayout的基本用法。
相对布局是什么
相对布局按照控件间的相对位置排列,比如控件A相对于控件B在控件B的左边,并且和控件B顶部对齐等。
相对布局有什么用
通过相对位置来控制控件在屏幕的位置。
相对布局怎么用
继续基于上一篇的项目,我们新建一个RelativeLayoutActivity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?
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
>
第一个控件
1
2
3
4
5
6
7
8
<
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
=
"我是第一个子控件"
/>
1
2
3
android:layout_height="100dp":表示TextView控件高度为100dp
android:layout_centerInParent="true":表示TextView控件位于父控件RelativeLayout的水平居中并且垂直居中
android:gravity="center":表示TextView控件上面显示的内容水平居中并且垂直居中
第二个控件
1
2
3
4
5
6
<
TextView
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignTop
=
"@+id/tv1"
android:layout_toLeftOf
=
"@+id/tv1"
android:text
=
"我是第二个子控件"
/>
1
2
android:layout_alignTop="@+id/tv1":表示与id为tv1控件顶部对齐
android:layout_toLeftOf="@+id/tv1":表示在id为tv1控件的左边
第三个控件
1
2
3
4
5
6
<
TextView
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignTop
=
"@+id/tv1"
android:layout_toRightOf
=
"@+id/tv1"
android:text
=
"我是第三个子控件"
/>
1
2
android:layout_alignTop ="@+id/tv1"表示与id为tv1控件顶部对齐
android:layout_toRightOf ="@+id/tv1"表示在id为tv1控件的右边
第四个控件
1
2
3
4
5
6
<
TextView
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignBottom
=
"@+id/tv1"
android:layout_toLeftOf
=
"@+id/tv1"
android:text
=
"我是第四个子控件"
/>
1
2
android:layout_alignBottom="@+id/tv1"表示与id为tv1控件底部对齐
android:layout_toLeftOf="@+id/tv1"表示在id为tv1控件的左边
第五个控件
1
2
3
4
5
6
<
TextView
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignBottom
=
"@+id/tv1"
android:layout_toRightOf
=
"@+id/tv1"
android:text
=
"我是第五个子控件"
/>
1
2
android:layout_alignBottom="@+id/tv1"表示与id为tv1控件底部对齐
android:layout_toRightOf="@+id/tv1"表示在id为tv1控件的右边
RelativeLayout常用基本属性
第一类属性 属性值为true或者false
1
2
3
4
5
6
7
8
9
10
11
12
13
android:layout_centerHrizontal:水平居中
android:layout_centerVertical:垂直居中
android:layout_centerInparent:相对于父控件完全居中
android:layout_alignParentBottom:贴紧父控件的下边缘
android:layout_alignParentLeft:贴紧父控件的左边缘
android:layout_alignParentRight:贴紧父控件的右边缘
android:layout_alignParentTop:贴紧父控件的上边缘
第二类属性 属性值必须为id的引用名“@id/id-name”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
android:layout_below:在某控件下方
android:layout_above:在某控件上方
android:layout_toLeftOf:在某控件的左边
android:layout_toRightOf:在某控件的右边
android:layout_alignTop:本控件的上边缘和某控件的上边缘对齐
android:layout_alignLeft:本控件的左边缘和某控件的左边缘对齐
android:layout_alignBottom:本控件的下边缘和某控件的下控件对齐
android:layout_alignRight:本控件的右边缘和某控
源码链接:https://yunjunet.cn/876760.html