我正在尝试用图像创建一个 GridLayout
。网格有 3 列。
不过我有几个问题:
ImageView
正在离开屏幕,如下图所示。GridLayout
未正确设置ImageView
的宽度以适应。
ImageView
之间没有空格/边距,即使我已经为设置了
。horizontalSpacing
和verticalSpacing
属性网格布局
这是我当前的代码。我应该更改什么来解决我的两个问题?
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth">
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:background="@color/Color_DarkRed" />
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:background="@color/Color_DarkRed" />
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:background="@color/Color_DarkRed" />
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:background="@color/Color_DarkRed" />
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:background="@color/Color_DarkRed" />
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:background="@color/Color_DarkRed" />
</GridLayout>
更新
正在关注 this answer ,我尝试使用以下布局:
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3">
<com.my.package.view.SquareImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Color_DarkRed" />
<com.my.package.view.SquareImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Color_DarkRed" />
<com.my.package.view.SquareImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Color_DarkRed" />
</GridLayout>
但它看起来像下图。每个 ImageView
占据屏幕的整个宽度:
最佳答案
你应该减少 layout_width="130dp"因为你的屏幕不能保证是 390dp
关于android - 图像离开屏幕的 GridLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44141778/