Android布局总结一:GridLayout布局(网格布局)

GridLayout布局简介

GridLayout布局是Android4.0(API Level 14)新引入的网格矩阵形式的布局控件。

GridLayout属性介绍

本身属性

  • android:alignmentMode
    说明:当设置alignMargins,使视图的外边界之间进行校准。可以取以下值:
    alignBounds – 对齐子视图边界。
    alignMargins – 对齐子视距内容。
  • android:columnCount
    说明:GridLayout的最大列数
  • android:rowCount
    说明:GridLayout的最大行数
  • android:columnOrderPreserved
    说明:当设置为true,使列边界显示的顺序和列索引的顺序相同。默认是true。
  • android:orientation
    说明:GridLayout中子元素的布局方向。有以下取值:
    horizontal – 水平布局。
    vertical – 竖直布局。
  • android:rowOrderPreserved
    说明:当设置为true,使行边界显示的顺序和行索引的顺序相同。默认是true。
  • android:useDefaultMargins
    说明: 当设置ture,当没有指定视图的布局参数时,告诉GridLayout使用默认的边距。默认值是false。

子元素属性

  • android:layout_column
    说明:显示该子控件的列,例如android:layout_column=”0”,表示当前子控件显示在第1列,android:layout_column=”1”,表示当前子控件显示在第2列。
  • android:layout_columnSpan
    说明:该控件所占的列数,例如android:layout_columnSpan=”2”,表示当前子控件占两列。

  • android:layout_row
    说明:显示该子控件的行,例如android:layout_row=”0”,表示当前子控件显示在第1行,android:layout_row=”1”,表示当前子控件显示在第2行。

  • android:layout_rowSpan
    说明:该控件所占的列数,例如android:layout_rowSpan=”2”,表示当前子控件占两行。

  • android:layout_columnWeight
    说明:该控件的列权重,与android:layout_weight类似,例如有GridLayout上两列,都设置android:layout_columnWeight = “1”,则两列各占GridLayout宽度的一半

  • android:layout_rowWeight
    说明:该控件的行权重,原理同android:layout_columnWeight。

平均分配格行/列的问题

GridLayout在Android 5.1(API Level 21)时引入了android:layout_columnWeight和android:layout_rowWeight来解决平分问题,但是API21前怎么办呢?

1.需要用到兼容包:

compile 'com.android.support:gridlayout-v7:22.+' 

2.布局中使用android.support.v7.widget.GridLayout

<android.support.v7.widget.GridLayout
    ...
 </android.support.v7.widget.GridLayout>

3.使用app:layout_columnWeight 和app:layout_rowWeight 设置权重

android:layout_rowSpan
android:layout_columnSpan
android:layout_columnWeight
android:layout_rowWeight
android:rowCount
android:columnCount

android:layout_rowSpan
android:layout_columnSpan
android:layout_columnWeight
android:layout_rowWeight
android:rowCout
android:columnCount
替换

4.举例:

<android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="10"
        app:rowCount="2"
        app:columnCount="2">
        <LinearLayout
            android:background="#ffffff"
            android:layout_margin="1dp"
            android:orientation="vertical"
            app:layout_rowWeight="1"
            app:layout_columnWeight="1"
            android:gravity="center">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/logo"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="设置"/>
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_margin="1dp"
            app:layout_rowWeight="1"
            app:layout_columnWeight="1"
            android:gravity="center"
            android:background="#ffffff">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/logo"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="帮助"/>
        </LinearLayout>
        <LinearLayout
            android:background="#ffffff"
            android:layout_margin="1dp"
            android:orientation="vertical"
            app:layout_rowWeight="1"
            app:layout_columnWeight="1"
            android:gravity="center">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/logo"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="退出"/>
        </LinearLayout>
        <LinearLayout
            android:background="#ffffff"
            android:layout_margin="1dp"
            android:orientation="vertical"
            app:layout_rowWeight="1"
            app:layout_columnWeight="1"
            android:gravity="center">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/logo"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="隐藏"/>
        </LinearLayout>
    </android.support.v7.widget.GridLayout>

这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值