Android五大布局詳解
Android 的五大布局分別是LinearLayout(線性布局)、FrameLayout(單幀布局)、RelativeLayout(相對布局)、 AbsoluteLayout(絕對布局)和TableLayout(表格布局),09年的時候,我就該總結啊!分享才有意義!下面分別介紹:
LinearLayout(最好少用):
LinearLayout按照垂直或者水平的順序依次排列子元素,每一個子元素都位于前一個元素之后。如果是垂直排列,那么將是一個N行單列的結構,每一行只會有一個元素,而不論這個元素的寬度為多少;如果是水平排列,那么將是一個單行N列的結構。如果搭建兩行兩列的結構,通常的方式是先垂直排列兩個元素,每一個元素里再包含一個LinearLayout進行水平排列。
<?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"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#fffedcba"
android:layout_weight="2" />
</LinearLayout>
</LinearLayout>
FrameLayout(比較少用):
FrameLayout是五大布局中最簡單的一個布局,在這個布局中,整個界面被當成一塊空白備用區域,所有的子元素都不能被指定放置的位置,它們統統放于這塊區域的左上角,并且后面的子元素直接覆蓋在前面的子元素之上,將前面的子元素部分和全部遮擋。顯示效果如下,第一個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"/>
</FrameLayout>
AbsoluteLayout(最好不用):
AbsoluteLayout是絕對位置布局,布局很容易花。在此布局中的子元素的android:layout_x和android:layout_y屬性將生效,用于描述該子元素的坐標位置。屏幕左上角為坐標原點(0,0),第一個0代表橫坐標,向右移動此值增大,第二個0代表縱坐標,向下移動,此值增大。在此布局中的子元素可以相互重疊。在實際開發中,通常不采用此布局格式,因為它的界面代碼過于剛性,以至于有可能不能很好的適配各種終端
RelativeLayout(最好多用):
RelativeLayout按照各子元素之間的位置關系完成布局。在此布局中的子元素里與位置相關的屬性將生效。例如android:layout_below, android:layout_above等。子元素就通過這些屬性和各自的ID配合指定位置關系。注意在指定位置關系時,引用的ID必須在引用之前,先被定義,否則將出現異常。RelativeLayout里常用的位置屬性如下:
android:layout_toLeftOf —— 該組件位于引用組件的左方
android:layout_toRightOf —— 該組件位于引用組件的右方
android:layout_above —— 該組件位于引用組件的上方
android:layout_below —— 該組件位于引用組件的下方
android:layout_alignParentLeft —— 該組件是否對齊父組件的左端
android:layout_alignParentRight —— 該組件是否齊其父組件的右端
android:layout_alignParentTop —— 該組件是否對齊父組件的頂部
android:layout_alignParentBottom —— 該組件是否對齊父組件的底部
android:layout_centerInParent —— 該組件是否相對于父組件居中
android:layout_centerHorizontal —— 該組件是否橫向居中
android:layout_centerVertical —— 該組件是否垂直居中
RelativeLayout是Android五大布局結構中最靈活的一種布局結構,適合一些復雜界面的布局
RelativeLayout
<?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">
<Button android:id="@+id/bt1" android:layout_width="50dp"
android:layout_height="50dp" android:background="#ffffffff" android:gravity="center"
android:layout_alignParentBottom="true" />
<Button android:id="@+id/bt2" android:layout_width="50dp"
android:layout_height="50dp" android:background="#ff654321" android:gravity="center"
android:layout_above="@id/bt1" android:layout_centerHorizontal="true" />
<Button android:id="@+id/bt3" android:layout_width="50dp"
android:layout_height="50dp" android:background="#fffedcba" android:gravity="center"
android:layout_toLeftOf="@id/bt2" android:layout_above="@id/text_01" />
</RelativeLayout>
TableLayout(少用):
TableLayout顧名思義,此布局為表格布局,適用于N行N列的布局格式。一個TableLayout由許多TableRow組成,一個TableRow就代表TableLayout中的一行。TableRow是LinearLayout的子類,它的android:orientation屬性值恒為horizontal,并且它的android:layout_width和android:layout_height屬性值恒為MATCH_PARENT和WRAP_CONTENT。所以它的子元素都是橫向排列,并且寬高一致的。這樣的設計使得每個TableRow里的子元素都相當于表格中的單元格一樣。在TableRow中,單元格可以為空,但是不能跨列。
margin是控件邊緣相對父控件,或者其他控件的邊距