android - 图像离开屏幕的 GridLayout

标签 android android-layout android-gridlayout

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

不过我有几个问题:

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

enter image description here

  1. ImageView 之间没有空格/边距,即使我已经为 设置了 horizo​​ntalSpacingverticalSpacing 属性网格布局

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

<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

最佳答案

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

关于android - 图像离开屏幕的 GridLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44141778/

相关文章:

java - Android:使用 setBackgroundDrawable 设置后位图变小

android - 为什么gradle无法找到com.google.dexmaker?

android - RecyclerView GridLayoutManager : how to auto-detect span count?

java - 调用抛出异常的方法时,是否必须在 Java 中包含 catch block ?

android - 无法运行 android/sdk/build-tools/23.0.2/aapt

java - fab.setVisibility(View.GONE) 不起作用

Android:在 gridlayout 或 gridview 中创建具有动态大小的网格

android - 自定义 View 的不同单元格大小网格

android - 提前获取星期几

java - 在 android studio 3.1.3 中集成任何库时无法解决依赖性错误