android4.0版本后新增了一個GridLayout肛响,它使用虛細線將布局劃分為行告喊、列和單元格麸拄,也支持一個控件在行、列上都有交錯排列黔姜,其實用方法和LinearLayout拢切,Relativelayout等類似,只不過多了一些特有的屬性秆吵。
GridLayout的布局策略簡單分為以下三個部分:
首先它與LinearLayout布局一樣淮椰,也分為水平和垂直兩種方式,默認是水平布 局纳寂,一個控件挨著一個控件從左到右依次排列主穗,但是通過指定android:columnCount設置列數(shù)的屬性后,控件會自動換行進行排列毙芜。另一方面黔牵, 對于GridLayout布局中的子控件,默認按照wrap_content的方式設置其顯示爷肝,這只需要在GridLayout布局中顯式聲明即可猾浦。
其次,若要指定某控件顯示在固定的行或列灯抛,只需設置該子控件的android:layout_row和android:layout_column屬性即 可金赦,但是需要注意:android:layout_row=”0”表示從第一行開始,android:layout_column=”0”表示從第一列開 始对嚼,這與編程語言中一維數(shù)組的賦值情況類似夹抗。
最后,如果需要設置某控件跨越多行或多列纵竖,只需將該子控件的android:layout_rowSpan或者layout_columnSpan屬性 設置為數(shù)值漠烧,再設置其layout_gravity屬性為fill即可,前一個設置表明該控件跨越的行數(shù)或列數(shù)靡砌,后一個設置表明該控件填滿所跨越的整行或 整列已脓。
可以說使用GridLayout以后可以完全不用tablelayout了,而且使用GridLayout有效減少了布局的深度通殃,提高了app整體的性能質(zhì)量度液。
下面是使用GridLayout完成的效果圖:
布局代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/digital_bg"
android:columnCount="6"
android:orientation="horizontal"
android:padding="16dip"
android:rowCount="3">
<TextView
android:id="@+id/edit_input"
android:layout_columnSpan="5"
android:layout_gravity="fill"
android:textSize="14sp"
android:gravity="center_vertical"
android:paddingLeft="8dip"
android:background="@drawable/input_bg" />
<ImageButton
android:id="@+id/delete"
android:layout_marginLeft="16dip"
android:background="@drawable/delete_btn_style" />
<ImageButton
android:id="@+id/num_1"
android:layout_marginTop="16dip"
android:background="@drawable/num_1_btn_style" />
<ImageButton
android:id="@+id/num_2"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:background="@drawable/num_2_btn_style" />
<ImageButton
android:id="@+id/num_3"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:background="@drawable/num_3_btn_style" />
<ImageButton
android:id="@+id/num_4"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:background="@drawable/num_4_btn_style" />
<ImageButton
android:id="@+id/num_5"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:background="@drawable/num_5_btn_style" />
<ImageButton
android:id="@+id/confirm"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:layout_rowSpan="2"
android:background="@drawable/confirm_btn_style" />
<ImageButton
android:id="@+id/num_6"
android:layout_marginTop="16dip"
android:background="@drawable/num_6_btn_style" />
<ImageButton
android:id="@+id/num_7"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:background="@drawable/num_7_btn_style" />
<ImageButton
android:id="@+id/num_8"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:background="@drawable/num_8_btn_style" />
<ImageButton
android:id="@+id/num_9"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:background="@drawable/num_9_btn_style" />
<ImageButton
android:id="@+id/num_0"
android:layout_marginLeft="16dip"
android:layout_marginTop="16dip"
android:background="@drawable/num_0_btn_style" />
</GridLayout>