Android基本佈局分別是:線性佈局LinearLayout、相對佈局RelativeLayout、幀佈局FrameLayout、表格佈局TableLayout、網格佈局GridLayout。
其中,表格佈局是線性佈局的子類。網格佈局是android 4.0後新增的佈局。
普通視圖還是佈局都繼承自 View ,其中 ViewGroup 就是所有佈局的父類, ViewGroup 繼承自 View 同時可以對 View 進行管理 ( 編排,控制 View 顯示位置和大小 )主要掌握以下三種佈局
LinearLayout(常用的佈局)
線性佈局,可以水平編排或者垂直編排孩子的顯示
android:orientation="vertical" 設置方向 vertical 垂直 ( 沿着 y 座標 ) horizontal 水平方向(沿着 x 座標)
線性佈局中可以使用 android:layout_weight 屬性設置權重,可以將 LinearLayout 中剩下的部分進行比例劃分
LinearLayout 中如果需要使用佔位視圖可以使用 Space
<Space
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="1" />
LinearLayout 中使用 android:gravity 可以調整孩子的對齊方式,但是要注意方向,垂直的 ( 如果高不定 ) ,可以調整孩子在 left\centerhorizontal\right 如果是水平 ( 寬如果不定 ) 可以調整孩子在 top\centervertical\bottom
RelativeLayout
相對佈局
第一種:子視圖相對於父容器,取值爲 true/false
android:layout_alignParentLeft="true" 靠父容器左側
android:layout_alignParentRight="true" 靠父容器右側
android:layout_alignParentTop="true" 靠父容器頂部
android:layout_alignParentBottom="true" 靠父容器底部
android:layout_centerVertical="true" 垂直居中
android:layout_centerInParent="true" 居中
android:layout_centerHorizontal="true" 水平居中
第二種:子視圖之間相互參考,值對方視圖的 id --> @id/xxx
id 的聲明: @+id/id 名稱 如: @+id/tv_a
id 的引用: @id/id 名稱 如: @id/tv_a
android:layout_toLeftOf 在誰的左側
android:layout_toRightOf 在誰的右側
android:layout_above 在誰的上面
android:layout_below 在誰的下面
android:layout_alignTop 頂部對齊
android:layout_alignLeft 左側對齊
android:layout_alignRight 右側對齊
android:layout_alignBottom 底部對齊
FrameLayout
幀佈局 ( 框架佈局 ) ,佈局特性是所有孩子默認疊在該容器左上角
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#55ff0000" />
<TextView
android:layout_width="150dp"
android:layout_height="100dp"
android:background="#5500ff00" />
<TextView
android:layout_width="100dp"
android:layout_height="150dp"
android:background="#550000ff" />
</FrameLayout>
其中孩子中可以使用 android:layout_gravity 來調整自己在父容器中的位置 ( 主動權在孩子身上 ) ,跟 android:gravity 不一樣的是 android:gravity 主動權在父元素身上
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#55ff0000"
android:text="ABC"
android:layout_gravity="center" /> 設置該視圖顯示在 FrameLayout 正中間
...
<TextView
android:layout_width="150dp"
android:layout_height="100dp"
android:background="#5500ff00"
android:layout_gravity="right|bottom" /> 顯示在右下角
</FrameLayout>
TableLayout
表格佈局繼承自LinearLayout,通過TableRow設置行,列數由TableRow中的子控件決定,
直接在TableLayout中添加子控件會佔據整個一行。
TableLayout常用屬性:
android:shrinkColumns:設置可收縮的列,內容過多就收縮顯示到第二行
android:stretchColumns:設置可伸展的列,將空白區域填充滿整個列
android:collapseColumns:設置要隱藏的列
列的索引從0開始,shrinkColumns和stretchColumns可以同時設置。
子控件常用屬性:
android:layout_column:第幾列
android:layout_span:佔據列數
GridLayout(網格佈局)
作爲android 4.0 後新增的一個佈局,與前面介紹過的TableLayout(表格佈局)其實有點大同小異;
不過新增了一些東西
1、跟LinearLayout(線性佈局)一樣,他可以設置容器中組件的對齊方式
2、容器中的組件可以跨多行也可以跨多列(相比TableLayout直接放組件,佔一行相比較)。
常用屬性:
排列對齊:
①設置組件的排列方式: android:orientation="" vertical(豎直,默認)或者horizontal(水平)
②設置組件的對齊方式: android:layout_gravity="" center,left,right,buttom
設置佈局爲幾行幾列:
①設置有多少行: android:rowCount="4" //設置網格佈局有4行
②設置有多少列: android:columnCount="4" //設置網格佈局有4列
設置某個組件位於幾行幾列
注:都是從0開始算的哦!
①組件在第幾行: android:layout_row = "1" //設置組件位於第二行
②組件在第幾列: android:layout_column = "2" //設置該組件位於第三列
設置某個組件橫跨幾行幾列:
①橫跨幾行: android:layout_rowSpan = "2" //縱向橫跨2行
②橫跨幾列: android:layout_columnSpan = "3" //橫向橫跨2列
最後說一點:
GridLayout是android 4.0 後才推出的,API Level 爲 14
如果讀者將佈局設置爲GridLayout時,會出現 莫名其妙的報錯,
只需要將配置文件中的 MinSDK改成14或者以上版本 即可,保存