FrameLayout(幀布局)和GridLayout(網(wǎng)格布局)也是Android開發(fā)會用到的布局,雖然不及LinearLayout和RelativeLayout使用頻率高,但還是有必要get一下。
FrameLayout幀布局
與其它布局相比,幀布局比較簡單,用法也比較單一。FrameLayout的屬性很少就兩個:
android:foreground設(shè)置幀布局容器的前景圖像android:foregroundGravity設(shè)置前景圖像顯示的位置
前景圖像:永遠(yuǎn)處于幀布局最上面,直接面對用戶的圖像,就是不會被覆蓋的圖片。
下面寫個示例體驗一下幀布局的效果:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="@mipmap/ic_launcher"
android:foregroundGravity="bottom|right">
<TextView
android:layout_width="240dp"
android:layout_height="240dp"
android:background="@color/teal_200"/>
<TextView
android:layout_width="160dp"
android:layout_height="160dp"
android:background="@color/purple_200"/>
<TextView
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@color/teal_700"/>
</FrameLayout>
效果預(yù)覽圖:

上圖右下角是幀布局的前景圖,而幀布局的子項自上而下的堆疊在一起。
GridLayout網(wǎng)格布局
GridLayout網(wǎng)格布局也很簡單,用來實現(xiàn)類似格子類的效果特別方便,用到的屬性如下:
android:orientation排列方向android:layout_gravity對齊方式android:columnCount="4"android:rowCount="4"幾行幾列android:layout_row="1"android:layout_column="2"子項在幾行幾列,索引從零開始android:layout_rowSpan="2"android:layout_columnSpan="1"子項占幾行幾列
下面寫個例子驗證一下上面的屬性:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:rowCount="4"
android:columnCount="4">
<Button
android:text="1.1"
android:textSize="18sp"/>
<Button
android:text="2行1列"
android:textSize="18sp"
android:layout_rowSpan="2"
android:layout_gravity="fill"
android:backgroundTint="@color/teal_700"/>
<Button
android:text="1.1"
android:textSize="18sp"/>
<Button
android:text="1行2列"
android:textSize="18sp"
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:backgroundTint="@color/teal_200"/>
<Button
android:text="2行2列"
android:textSize="18sp"
android:layout_rowSpan="2"
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:backgroundTint="@color/purple_200"/>
<Button
android:text="1.1"
android:textSize="18sp"/>
<Button
android:text="1.1"
android:textSize="18sp"/>
<Button
android:text="1.1"
android:textSize="18sp"/>
<Button
android:text="1.1"
android:textSize="18sp"/>
<Button
android:text="1.1"
android:textSize="18sp"/>
</GridLayout>
效果預(yù)覽圖:

上面示例簡單的展示了跨行跨列的效果。GridLayout 將容器切割為棋盤一樣4行4列的網(wǎng)格,每個網(wǎng)格可以放置一個組件,添加到容器的組件從左向右自上而下依次放置。GridLayout最大的特點是放置的組件自動占據(jù)網(wǎng)格的整個區(qū)域,每個組件的大小相同。不能改變組件大?。ǔ墙M件內(nèi)容撐開改變),只能改變組件之間的水平和垂直間隔。
若組件數(shù)超過網(wǎng)格設(shè)定的個數(shù),則布局管理器會自動增加網(wǎng)格個數(shù),原則是保持行數(shù)不變,增加列數(shù)來完成布局。當(dāng)容器大小發(fā)生改變時,各組件的相對位置不變,大小會改變。