【问题标题】:How to remove extra space in a gridview cell - Android如何删除 gridview 单元格中的额外空间 - Android
【发布时间】:2016-11-13 23:17:47
【问题描述】:

我正在 android studio 中制作自定义网格视图,每个单元格都包含一个图像视图和一个文本视图,但它在顶部和图像之间以及图像和文本之间提供了额外的空间,请参阅Image。我想摆脱多余的空间,但我想不出解决方案!

这里是Item(Cell)布局的代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:weightSum="5">

<ImageView
    android:id="@+id/picture"
    style="@style/Pic" />

<TextView
    android:id="@+id/label"
    style="@style/Text" />

这是我主要布局的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">

<TextView
    style="@style/Main_Text"
    android:text="@string/Main_Menu"/>

<GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/gridView"
    android:numColumns="3"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"/>

【问题讨论】:

  • android:adjustViewBounds="true" 添加到 ImageView。
  • 将 android:adjustViewBounds="true" 添加到 ImageView 并将 scaleType 设置为 'centerCrop'。它将删除多余的空间

标签: android android-layout android-studio gridview


【解决方案1】:

删除 weightsum 并在您的图像视图中添加

android:adjustViewBounds="true"
android:scaleType="fitXY"

如果您想使用 weightsum,请尝试为 imageview 和 textview 添加 layoutweight

【讨论】:

    【解决方案2】:

    尝试实现

    android:adjustViewBounds="true"
    

    【讨论】:

      【解决方案3】:

      从你的代码中删除 "android:verticalSpacing="0dip" 或者你可以试试这个,

      <GridView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/mGridView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@android:color/transparent"
      android:cacheColorHint="@android:color/transparent"
      android:columnWidth="40dip"
      android:horizontalSpacing="0dip"
      android:numColumns="auto_fit"
      android:scrollbars="none"
      android:verticalSpacing="0dip" />
      

      【讨论】: