最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Android開發(fā)學(xué)習(xí)教程(14)- Android布局之相對布局RelativeLayout

2023-01-27 16:07 作者:ChatGPT云炬學(xué)長  | 我要投稿

—— 珍貴的東西往往數(shù)目稀少,所以你要變得更強,才有力量去搶。

上一篇我們講了線性布局LinearLayout的基本用法,這里來學(xué)習(xí)常用布局之相對布局RelativeLayout的基本用法。

相對布局是什么

相對布局按照控件間的相對位置排列,比如控件A相對于控件B在控件B的左邊,并且和控件B頂部對齊等。

相對布局有什么用

通過相對位置來控制控件在屏幕的位置。

相對布局怎么用

繼續(xù)基于上一篇的項目,我們新建一個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控件上面顯示的內(nèi)容水平居中并且垂直居中

第二個控件

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

Android開發(fā)學(xué)習(xí)教程(14)- Android布局之相對布局RelativeLayout的評論 (共 條)

分享到微博請遵守國家法律
墨玉县| 中牟县| 鄂托克前旗| 汤阴县| 石嘴山市| 女性| 华安县| 阿坝县| 大方县| 南陵县| 南开区| 荃湾区| 曲沃县| 上栗县| 珲春市| 佛坪县| 佛学| 越西县| 白山市| 桑日县| 邵武市| 洛宁县| 新蔡县| 云和县| 进贤县| 丹阳市| 拜城县| 天津市| 土默特左旗| 开阳县| 灯塔市| 丰都县| 弥渡县| 营口市| 铁岭市| 湄潭县| 朝阳县| 甘南县| 宣化县| 高台县| 常德市|