天天看點

android GridLayout布局

  android4.0版本後新增了一個GridLayout,它使用虛細線将布局劃分為行、列和單元格,也支援一個控件在行、列上都有交錯排列,其實用方法和LinearLayout,Relativelayout等類似,隻不過多了一些特有的屬性。

GridLayout的布局政策簡單分為以下三個部分:

  首先它與LinearLayout布局一樣,也分為水準和垂直兩種方式,預設是水準布 局,一個控件挨着一個控件從左到右依次排列,但是通過指定android:columnCount設定列數的屬性後,控件會自動換行進行排列。另一方面, 對于GridLayout布局中的子控件,預設按照wrap_content的方式設定其顯示,這隻需要在GridLayout布局中顯式聲明即可。

      其次,若要指定某控件顯示在固定的行或列,隻需設定該子控件的android:layout_row和android:layout_column屬性即 可,但是需要注意:android:layout_row=”0”表示從第一行開始,android:layout_column=”0”表示從第一列開 始,這與程式設計語言中一維數組的指派情況類似。

 最後,如果需要設定某控件跨越多行或多列,隻需将該子控件的android:layout_rowSpan或者layout_columnSpan屬性

設定為數值,再設定其layout_gravity屬性為fill即可,前一個設定表明該控件跨越的行數或列數,後一個設定表明該控件填滿所跨越的整行或

整列。

  可以說使用GridLayout以後可以完全不用tablelayout了,而且使用GridLayout有效減少了布局的深度,提高了app整體的性能品質。

  下面是使用GridLayout完成的效果圖:

  

android 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>      

參考資料:http://blog.csdn.net/pku_android/article/details/7343258