gpt4 book ai didi

java - 我的网格布局中的 ImageView 溢出了最大宽度 - android studio?

转载 作者:行者123 更新时间:2023-12-01 17:29:07 25 4
gpt4 key购买 nike

我正在学习 Android 的 java 并根据教程使用网格布局和 ImageView 制作 connect 3 游戏。我已将网格布局设为正方形,并使宽度与主视图的宽度相匹配。网格布局为三列三行。

我将计数器添加到 ImageView 中,我在下面的代码中添加了三个计数器,但正如您所看到的,宽度与单元格不匹配并且超出了网格布局的宽度。我不想硬核 dp 值,因为我希望它适合所有屏幕。

有人可以建议该怎么做吗?

<androidx.gridlayout.widget.GridLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="272dp"
android:layout_marginBottom="48dp"
android:background="@drawable/board"
app:columnCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:rowCount="3">

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
app:layout_column="0"
app:layout_row="0"
app:srcCompat="@drawable/red" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
app:layout_column="1"
app:layout_row="0"
app:srcCompat="@drawable/red" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
app:layout_column="2"
app:layout_row="0"
app:srcCompat="@drawable/red" />



</androidx.gridlayout.widget.GridLayout>

看起来像这样(错误): Grid of counters

最佳答案

您可以添加权重并将其设置为 1 。并将所有宽度和高度设置为 0dp 。

<ImageView
//And you remaining code
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>

关于java - 我的网格布局中的 ImageView 溢出了最大宽度 - android studio?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61157851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com