【发布时间】:2017-10-23 19:37:56
【问题描述】:
我正在尝试使用图像创建 GridLayout。网格有 3 列。
我有几个问题:
-
ImageViews 正在离开屏幕,如下图所示。GridLayout未正确设置ImageViews 的宽度以适合。
-
ImageViews 之间没有空格/边距,即使我已为GridLayout设置了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 占据整个屏幕宽度:
【问题讨论】:
-
如果您可以使用线性布局而不是网格布局,那么您可以使用“权重属性”来线性适合您的元素。
-
您的屏幕不保证为 390dp(或 410 间距)宽...
-
@cricket_007 即使我将其更改为
20dp,它仍然不会拉伸图像以创建填充父级宽度的 3 列。
标签: android android-layout android-gridlayout