基于ConstraintLayout,如何在XML实现GridLayout单元格宽高一致
实现GridLayout网格项宽高一致的XML方案
当然可以纯靠XML实现你想要的效果!核心思路是结合GridLayout的列权重均分宽度,再利用ConstraintLayout的宽高比约束让高度自动匹配宽度,完全不用写Java/Kotlin代码,还能完美适配不同屏幕尺寸。
具体实现步骤
我直接给你完整的可运行代码示例,你可以直接参考修改:
<?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" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 外层GridLayout,设置列数和宽高 --> <androidx.gridlayout.widget.GridLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:columnCount="3" <!-- 这里设置你想要的列数 --> app:rowCount="2"> <!-- 第一个网格项:用ConstraintLayout包裹,实现正方形 --> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="0dp" android:layout_height="0dp" app:layout_columnWeight="1" <!-- 让列宽度均分屏幕 --> app:layout_rowWeight="1" <!-- 行高度也均分(可选,配合列权重更统一) --> app:layout_constraintDimensionRatio="1:1" <!-- 核心:宽高比1:1 --> android:background="#FFC107" android:layout_margin="4dp"> <!-- 这里放你需要的子View,比如文字、图标 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Item 1" app:layout_constraintCenterInParent="true"/> </androidx.constraintlayout.widget.ConstraintLayout> <!-- 第二个网格项 --> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="0dp" android:layout_height="0dp" app:layout_columnWeight="1" app:layout_rowWeight="1" app:layout_constraintDimensionRatio="1:1" android:background="#2196F3" android:layout_margin="4dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Item 2" app:layout_constraintCenterInParent="true"/> </androidx.constraintlayout.widget.ConstraintLayout> <!-- 更多网格项可以复制上面的结构 --> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="0dp" android:layout_height="0dp" app:layout_columnWeight="1" app:layout_rowWeight="1" app:layout_constraintDimensionRatio="1:1" android:background="#4CAF50" android:layout_margin="4dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Item 3" app:layout_constraintCenterInParent="true"/> </androidx.constraintlayout.widget.ConstraintLayout> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="0dp" android:layout_height="0dp" app:layout_columnWeight="1" app:layout_rowWeight="1" app:layout_constraintDimensionRatio="1:1" android:background="#F44336" android:layout_margin="4dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Item 4" app:layout_constraintCenterInParent="true"/> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.gridlayout.widget.GridLayout> </androidx.constraintlayout.widget.ConstraintLayout>
关键属性解释
app:layout_columnWeight="1":让每一列的宽度自动均分整个屏幕宽度,不管屏幕是宽是窄,每个网格项的宽度都能保持一致且铺满水平空间app:layout_constraintDimensionRatio="1:1":这是实现正方形的核心,告诉ConstraintLayout保持宽高比为1:1,这样高度会自动等于当前宽度android:layout_width="0dp"/android:layout_height="0dp":配合权重和比例约束,让系统自动计算最终的宽高尺寸,而不是固定值
适配旧版本支持库
如果你还在使用旧的android.support.v7.widget.GridLayout和android.support.constraint.ConstraintLayout,只需要把XML里的包名替换成对应的支持库路径就行,逻辑完全一样。
这种方案完全符合你的需求:水平均分屏幕、自适应不同尺寸、纯XML实现,不需要任何代码逻辑。
内容的提问来源于stack exchange,提问作者MM1010
相关产品推荐
相关产品推荐

