GridLayout(网格布局)
GridLayout(网格布局)是在Andriod4.0以后提出来的,它与表格布局管理器类似,但他更加灵活,在网格布局中,屏幕被分成很多行与列形成的单元格(可以自定义网格布局有多少行,多少列),每个单元格可以放置一个控件或者布局布局管理器,他的优势在于,不仅可以跨行,还可以跨列摆放组件。
优点:
1,当单元格大小大于组件大小时,可以通过设置layout_grativity属性值,设置组件在单元格里的位置
2,通过设置layout_columnWeight/layout_rowWeight属性值,可以设置各个组件的大小比例
3,可以直接设置组件位于某行某列
1.网格布局的语法格式
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
属性列表
>
</GridLayout >
2.GridLayout 支持的常用XML属性

- columnCount:---------------------------------指定网格的最大列数。
- orientatiopn:---------------------------------指定放入其中组件的排列方式
- rowCount:--------------------------------------指定网格最大行数
- userDefaultMargins:-------------------------指定是否使用默认边距
- android:alignmentMode:--------------------指定布局管理器的对其方式
- android:rowOrderPreserved:------------设置边界显示的顺序和列索引是否相同
- android:columnOrderPreserved:-------设置边界显示的顺序和列索引是否相同
3.为了控制各个组件的排列,网格布局管理器还提供了一个内部类LayoutParams,该类中提供XMl属性如下,组件属性
- android:layout_column:指定该组件位于网格的第几列
- android:layout_columnSpan:指定该组件横向跨几列(索引从0开始)
- android:layout_columnWeight:指定该组件列上的权重
- android:layout_gravity:指定组件采用什么方式占据网格空间
- android:layout_row:指定组件位于网格第几行
- android:layout_rowSpan:指定该组件纵向跨几列(索引从0开始)
- android:layout_rowWeight:指定该组件行上的权重
注:如果一个·组件设置跨行或跨列,需要先设置columnSpan或者rowSpan,然后再设置layout_gravity属性为fill,这样就可以充满横跨的行或列
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="6"
android:columnCount="4"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello"
android:layout_columnSpan="4"
android:layout_gravity="fill_vertical"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
>
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="()"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+/-"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Del"
android:layout_gravity="fill_horizontal"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
>
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="÷"
android:layout_gravity="fill_horizontal"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
>
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="×"
android:layout_gravity="fill_horizontal"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
>
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:layout_gravity="fill_horizontal"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
>
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:layout_gravity="fill_horizontal"
/>
</GridLayout>
效果

本文介绍了Android中的GridLayout布局管理器,它允许开发者灵活地将组件放置在网格中,支持跨行和跨列。关键特性包括设置组件在单元格的位置、大小比例以及直接定位组件。通过layout_columnCount、rowCount等属性可以定义网格布局,而LayoutParams则用于控制组件排列。示例代码展示了一个计算器应用的布局实现,展示了如何利用GridLayout创建一个按键布局。
2584

被折叠的 条评论
为什么被折叠?



