Android XML 布局基础(八)帧布局 - GridLayout

219 阅读1分钟

一、简介

  • 多种 Layout 布局是可以嵌套组合使用的。

  • GridLayout 属性说明

    名称含义
    android:columnCount="数字"列数
    android:rowCount="数字"行数
  • GridLayout 子视图属性说明

    名称含义
    android:layout_column="数字"表示显示在第几列,如果同时有多个视图设置为同一列,后面的视图将会被显示到下一行同样的列数位置
    android:layout_row="数字"表示显示在第几行,如果通过 layout_columnlayout_row 组合使用,将多个设置到通一个位置,则会叠加覆盖
    android:layout_columnSpan="数字"占用一行几个列位置
    android:layout_rowSpan="数字"占用一列的几个行位置
    android:layout_columnWeight="数字"该控件的列权重,与 android:layout_weight 类似,例如有 GridLayout 上两列,都设置 android:layout_columnWeight = "1",则两列各占 GridLayout 宽度的一半
    android:layout_rowWeight="数字"layout_columnWeight 同理

二、案例

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <!-- 网格布局 -->
    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:columnCount="4"
        android:rowCount="6"
        tools:ignore="MissingConstraints">
        <!-- 单个组件 -->
        <View
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="#ffc"/>
        <View
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="#fcf"/>
        <View
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="#cff"/>
        <View
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="#ccc"/>
        <!-- layout_rowSpan -->
        <View
            android:layout_rowSpan="2"
            android:layout_width="80dp"
            android:layout_height="160dp"
            android:background="#ddd"/>
        <View
            android:layout_rowSpan="2"
            android:layout_width="80dp"
            android:layout_height="160dp"
            android:background="#cdc"/>
        <!-- layout_columnSpan -->
        <View
            android:layout_columnSpan="2"
            android:layout_width="160dp"
            android:layout_height="80dp"
            android:background="#f88"/>
        <View
            android:layout_columnSpan="2"
            android:layout_width="160dp"
            android:layout_height="80dp"
            android:background="#f11"/>
    </GridLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

image.png