Android的界面是有佈局和組件協同完成的,佈局比如是建築裏的框架,而組件則至關於建築裏的磚瓦。組件按照佈局的要求依次排列,就組成了用戶所看見的界面。 html
全部的佈局方式均可以歸類爲ViewGroup的5個類別,即ViewGroup的5個直接子類。其它的一些佈局都擴展自這5個類。 android
1.LinearLayout,線性佈局方式 框架
這種佈局比較經常使用,也比較簡單,就是每一個元素佔一行,固然也可能聲明爲橫向排放,也就是每一個元素佔一列。 佈局
LinearLayout按照垂直或者水平的順序依次排列子元素,每個子元素都位於前一個元素以後。若是是垂直排列,那麼將是一個N行單列的結構,每一行只會有一個元素,而不論這個元素的寬度爲多少;若是是水平排列,那麼將是一個單行N列的結構。若是搭建兩行兩列的結構,一般的方式是先垂直排列兩個元素,每個元素裏再包含一個LinearLayout進行水平排列。 .net
LinearLayout中的子元素屬性android:layout_weight生效,它用於描述該子元素在剩餘空間中佔有的大小比例。加入一行只有一個文本框,那麼它的默認值就爲0,若是一行中有兩個等長的文本框,那麼他們的android:layout_weight值能夠是同爲1。若是一行中有兩個不等長的文本框,那麼他們的android:layout_weight值分別爲1和2,那麼第一個文本框將佔據剩餘空間的三分之二,第二個文本框將佔據剩餘空間中的三分之一。android:layout_weight遵循數值越小,重要度越高的原則。 code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff000000" android:text="@string/hello"/>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff654321" android:layout_weight="1" android:text="1"/>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#fffedcba" android:layout_weight="2" android:text="2"/>
</LinearLayout>
</LinearLayout>
2.Relative Layout,相對佈局 xml
RelativeLayout按照各子元素之間的位置關係完成佈局。在此佈局中的子元素裏與位置相關的屬性將生效。例如android:layout_below, android:layout_above, android:layout_centerVertical等。注意在指定位置關係時,引用的ID必須在引用以前,先被定義,不然將出現異常。 htm
RelativeLayout是Android五大布局結構中最靈活的一種佈局結構,比較適合一些複雜界面的佈局。 blog
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="@+id/text_01" android:layout_width="50dp" android:layout_height="50dp" android:background="#ffffffff" android:gravity="center" android:layout_alignParentBottom="true" android:text="1"/>
<TextView android:id="@+id/text_02" android:layout_width="50dp" android:layout_height="50dp" android:background="#ff654321" android:gravity="center" android:layout_above="@id/text_01" android:layout_centerHorizontal="true" android:text="2"/>
<TextView android:id="@+id/text_03" android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:layout_toLeftOf="@id/text_02" android:layout_above="@id/text_01" android:text="3"/>
</RelativeLayout>
3.AbsoluteLayout,絕對位置佈局 繼承
在此佈局中的子元素的android:layout_x和android:layout_y屬性將生效,用於描述該子元素的座標位置。屏幕左上角爲座標原點(0,0),第一個0表明橫座標,向右移動此值增大,第二個0表明縱座標,向下移動,此值增大。在此佈局中的子元素能夠相互重疊。在實際開發中,一般不採用此佈局格式,由於它的界面代碼過於剛性,以致於有可能不能很好的適配各類終端。
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#ffffffff" android:gravity="center" android:layout_x="50dp" android:layout_y="50dp" android:text="1"/>
<TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#ff654321" android:gravity="center" android:layout_x="25dp" android:layout_y="25dp" android:text="2"/>
<TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:layout_x="125dp" android:layout_y="125dp" android:text="3"/>
</AbsoluteLayout>
4.FrameLayout,幀佈局
FrameLayout是五大布局中最簡單的一個佈局,能夠說成是層佈局方式。在這個佈局中,整個界面被當成一塊空白備用區域,全部的子元素都不能被指定放置的位置,它們通通放於這塊區域的左上角,而且後面的子元素直接覆蓋在前面的子元素之上,將前面的子元素部分和所有遮擋。以下,第一個TextView被第二個TextView徹底遮擋,第三個TextView遮擋了第二個TextView的部分位置。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff000000" android:gravity="center" android:text="1"/>
<TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff654321" android:gravity="center" android:text="2"/>
<TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:text="3"/>
</FrameLayout>
5.TableLayout,表格佈局
適用於N行N列的佈局格式。一個TableLayout由許多TableRow組成,一個TableRow就表明TableLayout中的一行。
TableRow是LinearLayout的子類,ablelLayout並不須要明確地聲明包含多少行、多少列,而是經過TableRow,以及其餘組件來控制表格的行數和列數, TableRow也是容器,所以能夠向TableRow裏面添加其餘組件,沒添加一個組件該表格就增長一列。若是想TableLayout裏面添加組件,那麼該組件就直接佔用一行。在表格佈局中,列的寬度由該列中最寬的單元格決定,整個表格佈局的寬度取決於父容器的寬度(默認是佔滿父容器自己)。
TableLayout繼承了LinearLayout,所以他徹底能夠支持LinearLayout所支持的所有XML屬性,除此以外TableLayout還支持如下屬性:
XML屬性 相關用法 說明
1. andriod:collapseColumns setColumnsCollapsed(int ,boolean) 設置須要隱藏的列的序列號,多個用逗號隔開
2.android:shrinkColumns setShrinkAllColumns(boolean) 設置被收縮的列的序列號,多個用逗號隔開
3.android:stretchColimns setSretchAllColumnds(boolean) 設置容許被拉伸的列的序列號,多個用逗號隔開
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:background="#ffffffff" android:gravity="center" android:padding="10dp" android:text="1"/>
<TextView android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>
<TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>
<TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>
<TextView android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>
<TextView android:background="#ffffffff" android:gravity="center" android:padding="10dp" android:text="1"/>
</TableRow>
</TableLayout>
6.其餘佈局(隸屬關係請看上圖)
1)列表視圖(List View)
List View是可滾動的列表。以列表的形式展現具體內容,而且可以根據數據的長度自適應顯示。
具體應用請看:用法一 http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html
用法二 http://blog.csdn.net/koupoo/article/details/7018727
2)網格視圖(Grid View)
Grid View一個ViewGroup以網格顯示它的子視圖(view)元素,即二維的、滾動的網格。
具體應用查看:http://www.cnblogs.com/linzheng/archive/2011/01/19/1938760.html
3)標籤佈局(Tab Layout)
以標籤的方式顯示它的子視圖元素,就像在Firefox中的一個窗口中顯示多個網頁同樣。爲了狂建立一個標籤UI(tabbed UI),須要使用到TabHost
和TabWidget。
TabHost
必須是佈局的根節點,它包含爲了顯示標籤的TabWidget和顯示標籤內容的
FrameLayout
。
具體應用查看:http://www.cnblogs.com/devinzhang/archive/2012/01/18/2325887.html