UI组件——GridLayout

来源:互联网 发布:网络导报张海亮 编辑:程序博客网 时间:2024/05/22 04:57
1 GridLayout(网格布局) 在Android4.0开始引入。
GridLayout可以把布局分为几行和几列。
GridLayout可以设置一个组件横框多少行,纵夸多少列。
GridLayout和TableLayout很相似,但是如果要设计行列布局,GridLayout使用更方便、更高效。因为:
GridLayout works with a flat-view hierarchy, where child views set their locations in the grid by specifying the rows and columns they should be in. By maintaining a flat hierarchy, GridLayout is able to more swiftly layout its child views. 
The new GridLayout control for Android 4.0 is very powerful and we've just scratched the surface of what it can do

2 GridLayout常见属性
GridLayout常用的XML属性和方法说明
XML属性相关方法说明android:alignmentModesetAlignmentMode(int)设置该布局管理器采用的对齐模式
android:rowCount
setRowCount(int)
设置该网格的行数
android:columnCountsetColumnCount(int)设置该网格的列数量
android:rowOrderPreserved
setRowOrderPreserved(boolean)
设置该网格容器是否保留行序号
android:columnOrderPreservedsetColumnOrderPreserved(boolean)设置该网格容器是否保留序列号android:useDefaultMarginssetUseDefaultMargins(boolean)设置该布局管理器是否使用默认的页边距


GridLayout.LayoutParams常用的XML属性和方法说明
XML属性
相关方法 
说明android:layout_column
设置该组件在GridLayout的第几列android:layout_columnSpan
设置该子组件在GridLayout横向上跨几列android:layout_gravity
设置该子组件采用何种方式占据该网格的空间android:layout_row
设置该子组件在GridLayout的第几行android:layout_rowSpan
设置该子组件在GridLayout纵向上跨几行
android:orientation
说明:
(1) Gridlayout 的默认对齐方式是android:orientation="horizontal"。
<?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:rowCount="2"
        android:columnCount="2">
     <TextView
            android:text="Cell 0"
            android:textSize="14dip" />
     <TextView
            android:text="Cell 1"
            android:textSize="14dip" />
     <TextView
            android:text="Cell 2"
            android:textSize="14dip" />
     <TextView
            android:text="Cell 3"
            android:textSize="14dip" />
</GridLayout>

The layout will adjust the row and column sizes so that the cells can fit their content, as illustrated by the following diagram:
 


<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"    
        android:rowCount="2"
        android:columnCount="2"
        android:orientation="vertical">
</GridLayout>

Now, the GridLayout will position the cells from top to bottom in each column, instead of left to right, as shown below:
(2) layout_gravity,android:layout_row , android:layout_column and layout_textSise等attribute can also adjust the size。
(3) layout_gravity attribute can also adjust the size。

    <Button
        android:layout_columnSpan="2"
        android:text="0"/>

    <Button
        android:layout_columnSpan="3"
        android:layout_gravity="fill"
        android:text="="/>

Ref:
http://code.tutsplus.com/tutorials/android-user-interface-design-creating-a-numeric-keypad-with-gridlayout--mobile-8677
http://www.tuicool.com/articles/Rr2EfmA
0 0