AbsoluteLayout因為已被廢除,因此不做介紹;
只要存在介面,就會有布局的存在,就像Swing,雖然一個是案頭應用,一個是手機應用,但是他們都差不多。
1.LinearLayout
預設布局。組件的排列按照預先定義方向很有序的排列,類似於Swing中的FlowLayout;
注意點:
(1)可以在<LinearLayout>中添加android:orientation:vertical/horizontal ;(2)可以嵌套<LinearLayout>;
2.FrameLayout
每個組件都在左上方,如果多個組件一起出現,則會重疊,類似於git疊加的動畫;
應用:線上視頻播放器
當點擊按鈕,則開始播放視頻;main.xml
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/frame3" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/frame2" android:layout_gravity="center"//置中 /></FrameLayout>
3.RelativeLayout
每個組件定位都是按照與其他組件的上下、左右定位;預設的定位為左上方;
(1)定位與組件的上下左右android:layout_below="@id/.."android:layout_above="@id/"android:layout_toLeftOf="@id/"android:layout_toRightOf="@id/"(2)定位與組件的邊緣對齊android:layout_alignLeft="@id/"android:layout_alignRight="@id/"android:layout_alignTop="@id/"android:layout_alignBottom="@id/"(3)定位與父組件的邊緣對齊android:layout_alignParentTop="true"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"android:layout_alignParentRight="true"(4)與整個螢幕的關係android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:layout_centerInParent="true"(5)組件間的距離android:layout_marginTop="5dp"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:layout_marginBottom="5dp"android:layout_margin="5dp"
4.TableLayout
類似於Swing中的GridLayout;
表格版面配置的每行用<TabRow>括起來;在<TableLayout>中可以定義如下屬性:
(1)android:shrinkColumns="1" 表明第2個控制項如果裡面的內容過多,會收縮,擴充到第二行,而不是延伸;(2)android:stretchColumns="2" 如果有空白,第3個控制項填充;
在控制項中設定:(1)android:layout_column="2" 將此控制項放在第3個位置;(2)android:layout_span="2" 此控制項佔據2個單元位置;
Tips:簡單布局工具droiddrawhttp://code.google.com/p/droiddraw/downloads/detail?name=droiddraw-r1b22.zip&can=2&q=