【问题标题】:Unwanted Gridview spacing不需要的 Gridview 间距
【发布时间】:2015-09-30 23:59:39
【问题描述】:

我的网格设计就是这样。但我想删除网格元素两侧的空格。 (黑色标记)

我希望图像有点像放大

gridview 的 XML 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="100dip"
        android:gravity="center"
        android:numColumns="2"
        android:stretchMode="spacingWidthUniform" />

</LinearLayout>

我的网格元素布局

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

    <ImageView
        android:id="@+id/grid_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        android:contentDescription="@string/im" >
    </ImageView>

    <TextView
        android:id="@+id/grid_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:maxLength="23"
        android:gravity="center"
        android:layout_marginTop="10sp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:textSize="12sp" >
    </TextView>

</LinearLayout>

【问题讨论】:

    标签: android android-imageview textview android-gridview


    【解决方案1】:

    尝试改变

     <GridView
            android:id="@+id/grid"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:verticalSpacing="1dp"
            android:horizontalSpacing="1dp"
            android:numColumns="2"
            android:background="#abcdef"
            android:stretchMode="columnWidth" />
    

    【讨论】:

    • 那行得通,有没有办法在列之间引入分隔符,如线或细空间。
    • 你可以做的是将背景颜色设置为gridview并将垂直和水平间距设置为1dp,我已经更新了我的答案
    • 确实如此,但左边没有间距(我已经更新了图片)。此外,我不仅想水平扩展图像,还想扩展正方形,使其看起来不会失真。
    • 你需要创建自定义的 imagview 看这个链接stackoverflow.com/questions/16506275/…
    【解决方案2】:

    您可以将 GridView 替换为 GridLayout 并使用权重使图像填满屏幕。以下是参考:http://developer.android.com/reference/android/widget/GridLayout.html,(参见章节:多余空间分布)

    【讨论】: