You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询Android Studio 3.1中GridLayout的名称及使用方法

Android Studio 3.1中GridLayout的名称及使用指南

嘿,我来帮你理清Android Studio 3.1里GridLayout的相关问题~

GridLayout的名称

在AS 3.1版本中,它的官方名称还是GridLayout,不过有两个不同的包路径可选,你可以根据项目需求来选:

  • 原生框架版本:android.widget.GridLayout,适合基于旧版Android SDK开发的项目
  • AndroidX兼容库版本:androidx.gridlayout.widget.GridLayout,更推荐这个,因为能适配更多设备,AS 3.1已经支持AndroidX的相关配置啦

如何使用GridLayout

我分两种常用场景给你说明:

1. 在XML布局中使用

首先如果用AndroidX版本,确保Module级的build.gradle里添加了依赖(如果创建项目时选了AndroidX,一般会自动加):

implementation 'androidx.gridlayout:gridlayout:1.0.0'

然后就可以在XML里写布局了,比如:

<androidx.gridlayout.widget.GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:columnCount="3" <!-- 设置总列数 -->
    app:rowCount="2"    <!-- 设置总行数 -->
    app:useDefaultMargins="true"> <!-- 开启默认边距,让子View不会挤在一起 -->

    <!-- 第一个子View,放在第0行第0列 -->
    <TextView
        android:text="Item 1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_column="0"
        app:layout_row="0"/>

    <!-- 第二个子View,第0行第1列 -->
    <TextView
        android:text="Item 2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_column="1"
        app:layout_row="0"/>

    <!-- 第三个子View,第0行第2列 -->
    <TextView
        android:text="Item 3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_column="2"
        app:layout_row="0"/>

    <!-- 第四个子View,跨2列显示 -->
    <TextView
        android:text="Item 4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_column="0"
        app:layout_row="1"
        app:layout_columnSpan="2"/>

</androidx.gridlayout.widget.GridLayout>

要是你用原生框架版本,把包名换成android.widget.GridLayout,对应的属性前缀改成android:就行,比如android:columnCount。

2. 在代码中动态创建

如果需要动态生成GridLayout,也很简单,以AndroidX版本为例:

import androidx.gridlayout.widget.GridLayout;
import android.widget.TextView;
import android.view.ViewGroup;

// 假设在Activity里
GridLayout gridLayout = new GridLayout(this);
// 设置布局参数
gridLayout.setLayoutParams(new ViewGroup.LayoutParams(
    ViewGroup.LayoutParams.MATCH_PARENT,
    ViewGroup.LayoutParams.MATCH_PARENT
));
gridLayout.setColumnCount(3);
gridLayout.setRowCount(2);
gridLayout.setUseDefaultMargins(true);

// 添加第一个子View
TextView item1 = new TextView(this);
item1.setText("Item 1");
GridLayout.LayoutParams params1 = new GridLayout.LayoutParams();
params1.columnSpec = GridLayout.spec(0); // 列索引0
params1.rowSpec = GridLayout.spec(0);    // 行索引0
gridLayout.addView(item1, params1);

// 其他子View照着这个逻辑添加就行...

// 最后把GridLayout加到根布局里
ViewGroup rootView = findViewById(android.R.id.content);
rootView.addView(gridLayout);

另外提个小技巧:在AS 3.1的可视化布局编辑器里,你可以直接从Palette面板的「Layouts」分类里找到GridLayout,拖拽到布局后,右侧属性面板就能直接调整列数、行数这些参数,可视化操作更直观哦~

内容的提问来源于stack exchange,提问作者Meylis Toylyyev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:39:21