Pro Android学习笔记(二八):用户界面和控制(16):GridLayout

来源:互联网 发布:电脑有些什么软件 编辑:程序博客网 时间:2024/06/14 15:14

网格布局:GridLayout

我个人觉得GridLayout的设计还不很完善,每个网格的大小,由填充的cell决定,即默认是wrap很容易整个GridLayout超出屏幕。下面是一个例子:

<?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:orientation="vertical"
    android:rowCount="4"
    android:columnCount="2"> <!-- 设计4×2的表格 -->
    <ImageView android:layout_row="0"
       android:layout_column="1"
        android:scaleType="fitCenter"
        android:src="@drawable/png02"/> <!-- 可以指定放置的位置 -->
    <ImageView android:layout_row="1"
       android:layout_column="0"
        android:scaleType="fitCenter"
        android:src="@drawable/png04"/>
    <ImageView android:layout_row="2"
        android:layout_column="1"

        android:scaleType="fitCenter"
        android:src="@drawable/png08"/>
    <ImageView android:layout_row="3"
        android:layout_column="0"
        android:scaleType="fitCenter"
        android:src="@drawable/png18"/>
</GridLayout>

GridLayout的灵活支出在于可以指定view方式的位置,运行有些问题不放置内容,如上面的例子。行号和列号均从0开始计算。此外,如果一个cell需要占据多个位置,可以用android:layout_rowspan和android:layout_columnspan来设置。

GridLayout最大的问题就是整个表格的所占空间不确定,行距由该行中最大的cell的高度决定,列距由该列最宽的cell决定。在上面的例子中,有部分控件超出了屏幕。每个cell的大小缺省为wrap_content,如果我们设置为match_parent,则该cell的大小为整屏。

相关链接: 我的Android开发相关文章

原创粉丝点击