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控件高度为100dpandroid: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

