【個人總結】Android幾種常用佈局的總結

1、線性佈局(LinearLayout):框架內的元素在橫向或縱向呈線性排布。該框架可通過orientation控制現線性方向,通過layout_weight控制一行中各個元素的比例。一個LinearLayout元素就是一行或一列(看orientation決定)。
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
44
45
46
47
48
49
50
51
52
53
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".LinearLayoutActivity">
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:background="#ff0000"
android:orientation="vertical">"
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:background="#00ff00"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:background="#0000ff"/>
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00ff00"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:background="#ff0000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:background="#0000ff"/>
</LinearLayout>"
</LinearLayout>
2、框架佈局(FrameLayout):該框架是一個層疊的佈局,放上去的元素是呈一層一層分佈的,主要用來放置需要重疊的元素,比如圖片等。
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
<!--  
FrameLayout內的元素佈局的方法有限,只要是控制是否居中和控制與邊緣的距離
-->
<FrameLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_below="@id/button4"
android:layout_alignLeft="@id/button4"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#000000">
<ImageView
android:id="@+id/kobe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/kobe"
android:scrollbars="vertical"
android:contentDescription="kobe"/>
<ImageView
android:id="@+id/robot"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:src="@drawable/androidrobot"/>
</FrameLayout>
3、相對佈局(RelativeLayout):最常用的一種佈局,可根據控制元素與元素的相對距離,相對比較靈活,一般要決定一個元素的距離,需要兩個相對位置才能比較準確確定,就跟兩點座標確定一個距離位置是一個道理。控制相對位置的屬性有很多,常用的有toLeftOf,toRightOf,above,below,alignLeft,alignRight,alignTop,alignBottom等等。
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
44
45
46
47
48
49
50
51
52
53
54
55
56
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/button1"
android:layout_toRightOf="@id/button1"
android:text="button2"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/button2"
android:layout_toRightOf="@id/button2"
android:text="button3"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/button1"
android:layout_below="@id/button1"
android:text="button4"/>
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/button4"
android:layout_toRightOf="@id/button4"
android:text="button5"/>
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/button5"
android:layout_toRightOf="@id/button5"
android:text="button6"/>
</RelativeLayout>

4、表格佈局(TableLayout): 該佈局是繼承了線性佈局,可控制多行多列,可控制行是否可隱藏,是否可伸縮。StretchColumn表示可伸展列,元素不夠列寬度自動伸展補齊,ShrinkColumn表示可收縮列,元素不夠或超出列寬度會自動收縮,CollapseColumn表示可隱藏列。可通過這幾個屬性控制有幾列,然後加入子標籤<TableRow>添加行,幾個<TableRow>表示幾行。


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
44
45
46
47
48
49
50
<TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="0,1,2"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<!-- 應爲該佈局採用的是stretchColumn伸展列,所以如果每列的元素寬度不夠會自動延伸使之充滿每列的寬度 -->
<TableRow
android:id="@+id/tr1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#234875">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"/>
</TableRow>
<!-- 沒定義TableRow來控制每一行則會把該元素當做是一整行的內容 -->
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"/>
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"/>
</TableLayout>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章