我正在尝试将 gridView 用于具有不同棋盘尺寸的游戏。我以编程方式为 GridView 设置列数,每列都有一个固定大小的按钮。GridView 宽度在 xml 中设置为“wrap_content”。但是 - gridView 始终作为“match_parent”传播到整个屏幕。这里看起来如何:
这是我的 GridView xml(它在约束布局中)。我已经尝试过增加重量或用线性布局包裹它。我添加了: android:gravity="center" 和 android:layout_centerInParent="true" 另外我尝试更改stretchMode,但它只是让列之间的间距很大,或者当我真的让它们成为矩形时想让他们保持方正。
<GridView
android:id="@+id/gridView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="24dp"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="32dp"
android:layout_weight="0.5"
android:columnWidth="64dp"
android:gravity="center"
android:layout_centerInParent="true"
android:horizontalSpacing="2dp"
android:stretchMode="none"
android:verticalSpacing="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2"></GridView>
我在网上找不到任何可行的解决方案,希望您能提供帮助,谢谢!