【问题标题】:GridLayout with images going off screenGridLayout,图像离开屏幕
【发布时间】:2017-10-23 19:37:56
【问题描述】:

我正在尝试使用图像创建 GridLayout。网格有 3 列。

我有几个问题:

  1. ImageViews 正在离开屏幕,如下图所示。 GridLayout 未正确设置 ImageViews 的宽度以适合。

  1. ImageViews 之间没有空格/边距,即使我已为 GridLayout 设置了 horizontalSpacingverticalSpacing 属性。

这是我当前的代码。我应该改变什么来解决我的两个问题?

<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 占据整个屏幕宽度:

http://i.imgur.com/p8AXcKN.png

【问题讨论】:

  • 如果您可以使用线性布局而不是网格布局,那么您可以使用“权重属性”来线性适合您的元素。
  • 您的屏幕不保证为 390dp(或 410 间距)宽...
  • @cricket_007 即使我将其更改为20dp,它仍然不会拉伸图像以创建填充父级宽度的 3 列。

标签: android android-layout android-gridlayout


【解决方案1】:

你应该减少 layout_width="130dp" 因为你的屏幕不能保证是 390dp

【讨论】:

  • 好的,但是即使我将其更改为20dp,它也不会拉伸以适应GridLayout 的宽度以形成3 列。
【解决方案2】:

更改图像,或者您也可以使用方形图像视图来调整网格项目的大小。

创建一个单独的类来动态定义您的 ImageView 的大小,该类应继承 ImageView 类,如下所示:

    public class SquareImageView extends ImageView {

    public SquareImageView(Context context) {
        super(context);
    }

    public SquareImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);

    int width = getMeasuredWidth();
        setMeasuredDimension(width, width);

    }
}

setMeasuredDimension(width, width);将自动将您的高度设置为宽度。

在 xml 文件中,使用该类作为视图代替 ImageView,如下所示:

<com.packagepath.SquareImageView
    android:id="@+id/Imageview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

【讨论】:

  • 刚刚试过这个。仍然遇到问题。我更新了问题的底部,以便您可以看到仍然存在的问题。
  • 用fill_parent改变match_parent
猜你喜欢
  • 2020-12-19
  • 2021-10-23
  • 2019-12-08
  • 2011-09-14
  • 2018-08-11
  • 2015-12-14
  • 1970-01-01
  • 1970-01-01
  • 2020-06-13
相关资源
最近更新 更多