您的位置:首页 > 产品设计 > UI/UE

UI组件——GridLayout

2016-04-18 00:00 816 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: