布局介紹
Android布局是應(yīng)用界面開發(fā)的重要一環(huán),在Android中,共有四種布局方式,
分別是:FrameLayout( 幀布局 )、LinearLayout (線性布局)、TableLayout(表格布局)、RelativeLayout(相對布局)。
1.LinearLayout
線性布局LinearLayout 在單一方向上對齊所有的子視圖-豎向或者橫向,這依賴于你怎么定義方向orientation 屬性。所有子視圖依次堆積,所以一個豎向列表每行只有一個子視圖,不管它們有多寬,而一個橫向列表將只有一行高(最高子視圖的高度,加上填充)。一個線性布局LinearLayout 會考慮子視圖之間的邊緣空白margins以及每個子視圖的引力屬性(靠右,居中,或者靠左)。
2.RelativeLayout
相對布局RelativeLayout允許子視圖指定它們和父視圖或彼此之間的相對位置(通過ID指定)。因此你可以按正確的順序?qū)R兩個元素,或者讓一個視圖在另外一個下面,居于屏幕中間,左邊的中間,等等。元素通過給定順序來繪制,因此如果這第一個元素在屏幕中間,其他以它對齊的元素都會對齊到屏幕中間。同樣,因為這個順序,如果使用XML來指定這個布局,你將引用的元素(為了定位其它視圖對象)必須被列在XML文件中,在你通過引用ID從其他視圖中引用它之前。
其中一些特性直接由元素支持,另外一些由它的LayoutParams成員變量支持(為所有這個屏幕中的元素子類化RelativeLayout,因為所有元素都是RelativeLayout父對象的子元素)。已定義的相對布局RelativeLayout參數(shù)是:width,height,below,alignTop,toLeft,padding[Bottom|Left|Right|Top],以及 margin[Bottom|Left|Right|Top]。注意其中一些參數(shù)明確支持相對布局位置-它們的數(shù)值必須是你的相對位置元素的ID。
1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="wrap_content"
5 android:background="@drawable/blue"
6 android:padding="10px" >
7
8 <TextView android:id="@+id/label"
9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 android:text="Type here:" />
12
13 <EditText android:id="@+id/entry"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content"
16 android:background="@android:drawable/editbox_background"
17 android:layout_below="@id/label" />
18
19 <Button android:id="@+id/ok"
20 android:layout_width="wrap_content"
21 android:layout_height="wrap_content"
22 android:layout_below="@id/entry"
23 android:layout_alignParentRight="true"
24 android:layout_marginLeft="10px"
25 android:text="OK" />
26
27 <Button android:layout_width="wrap_content"
28 android:layout_height="wrap_content"
29 android:layout_toLeftOf="@id/ok"
30 android:layout_alignTop="@id/ok"
31 android:text="Cancel" />
32 </RelativeLayout>
33
3.TableLayout
表格布局TableLayout把它的子視圖定位到行和列中。表格布局容器不顯示行,列和單元的邊界線。表的列和最多行單元數(shù)一樣多。一個表可以有空單元,但是單元不能像HTML里面那樣跨列。
TableRow 對象是一個TableLayout的子視圖(每個TableRow定義了表中的一個單獨行)。每行有0或多個單元,可用任何其他視圖定義。因此,行單元可能由各個視圖對象組成,如ImageView或TextView對象。一個單元也可以是一個ViewGroup對象(比如,你可以嵌入另一個表布局作為一個單元)。
1 <?xml version="1.0" encoding="utf-8"?>
2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:stretchColumns="1">
6 <TableRow>
7 <TextView
8 android:text="@string/table_layout_4_open"
9 android:padding="3dip" />
10 <TextView
11 android:text="@string/table_layout_4_open_shortcut"
12 android:gravity="right"
13 android:padding="3dip" />
14 </TableRow>
15
16 <TableRow>
17 <TextView
18 android:text="@string/table_layout_4_save"
19 android:padding="3dip" />
20 <TextView
21 android:text="@string/table_layout_4_save_shortcut"
22 android:gravity="right"
23 android:padding="3dip" />
24 </TableRow>
25 </TableLayout>
4.FrameLayout
FrameLayout是最簡單的一個布局對象。它被定制為你屏幕上的一個空白備用區(qū)域,之后你可以在其中填充一個單一對象 — 比如,一張你要發(fā)布的圖片。所有的子元素將會固定在屏幕的左上角;你不能為FrameLayout中的一個子元素指定一個位置。后一個子元素將會直接在前一個子元素之上進(jìn)行覆蓋填充,把它們部份或全部擋?。ǔ呛笠粋€子元素是透明的)。
main.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent">
5
6 <ImageView
7 android:layout_width="fill_parent"
8 android:layout_height="wrap_content"
9 android:src="@drawable/movie"
10 android:contentDescription="@string/movie"/>
11 <ImageView
12 android:layout_width="wrap_content"
13 android:layout_height="wrap_content"
14 android:src="@drawable/paly"
15 android:layout_gravity="center"
16 android:contentDescription="@string/play"/>
17 </FrameLayout>
string.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3
4 <string name="hello">Hello World, FrameLayoutActivity!</string>
5 <string name="app_name">FrameLayout布局</string>
6 <string name="movie">復(fù)仇者聯(lián)盟</string>
7 <string name="play">播放按鈕</string>
8
9 </resources>