LinearLayout : 線性布局
在一般情況下,當有很多控件需要在一個界面列出來時,我們就可以使用線性布局(LinearLayout)了, 線性布局是按照垂直方向(vertical)或水平方向(horizontal)的順序依次排序子元素,每一個子元素都位于前一個元素之后,下面我們就簡單的了解一下吧
在XML布局文件中會遇到如下一些單位
px:是屏幕的像素點 dp:一個基于density的抽象單位,屏幕的物理尺寸 sp:同dp相似,但會根據用戶的字體大小進行縮放
XML代碼如下:改變一下android:orientation="vertical" 垂直方向(vertical)
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
|
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = "com.example.administrator.adapter.MainActivity" > < TextView android:text = "第一個TextView" android:background = "#ef0808" android:gravity = "center" android:textSize = "18sp" android:layout_width = "match_parent" android:layout_height = "100dp" /> < TextView android:text = "第二個TextView" android:gravity = "center" android:background = "#31ef0b" android:textSize = "18sp" android:layout_width = "match_parent" android:layout_height = "100dp" /> < TextView android:text = "第三個TextView" android:gravity = "center" android:textSize = "18sp" android:background = "#ec07ca" android:layout_width = "match_parent" android:layout_height = "100dp" /> < TextView android:text = "第四個TextView" android:gravity = "center" android:textSize = "18sp" android:background = "#f5d105" android:layout_width = "match_parent" android:layout_height = "100dp" /> </ LinearLayout > |
運行結果:每一個TextView都從上往下依次排列
XML代碼如下:改變一下android:orientation="horizontal" 水平方向(horizontal)
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
|
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "horizontal" tools:context = "com.example.administrator.adapter.MainActivity" > < TextView android:text = "第一個TextView" android:background = "#ef0808" android:gravity = "center" android:textSize = "18sp" android:layout_width = "100dp" android:layout_height = "100dp" /> < TextView android:text = "第二個TextView" android:gravity = "center" android:background = "#31ef0b" android:textSize = "18sp" android:layout_width = "100dp" android:layout_height = "100dp" /> < TextView android:text = "第三個TextView" android:gravity = "center" android:textSize = "18sp" android:background = "#ec07ca" android:layout_width = "100dp" android:layout_height = "100dp" /> < TextView android:text = "第四個TextView" android:gravity = "center" android:textSize = "18sp" android:background = "#f5d105" android:layout_width = "100dp" android:layout_height = "100dp" /> </ LinearLayout > |
運行結果:每一個TextView都從左向右依次水平排列
這兩種線性布局唯一的差別就是android:orientation的值不同
實驗總結:這兩種線性布局唯一的差別就是android:orientation的值不同, 通過本次實驗對Android中的線性布局有了一個初步的了解
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!
原文鏈接:http://www.cnblogs.com/bigbossOne/p/6259937.html