android gridview行分隔符/分隔符

Fra*_*ank 34 android gridview

有没有办法在gridview中的行之间显示(水平)分隔线?

我尝试在每个网格项下面放置一个小的分隔图像,但这不是一个解决方案,因为当一行未完全填充项目时,它不会跨越整行.

有没有办法在每一行之间添加图像?我只能找到改变行之间空间的方法.

Cha*_*kar 81

如果您对网格项使用自定义布局.下面的代码将起作用.

第1步:为GridView提供背景颜色

这将作为分隔符.
给予horizontalSpacingverticalSpacing作为1dp
backgroundColor将是你的分频器颜色.

<GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#e5e5e5"
        android:horizontalSpacing="1dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="1dp" >
Run Code Online (Sandbox Code Playgroud)

第2步:为自定义网格项布局提供背景颜色

这将作为GridItems的前景色.
在我的情况下,我保持白色(#fff)

<?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"
    android:gravity="center"
    android:background="#fff"
    android:padding="15dp"
     >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_launcher_transparent" />

    <TextView
        android:id="@+id/lable"
        android:layout_marginTop="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textStyle="bold"
        android:textColor="#D0583B"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

结果

在此输入图像描述

注意:
如果您不想要垂直分隔符,请保留horizontalSpacing = 0dp
如果您不想要水平分隔符,请保留verticalSpacing = 0dp

  • 如果 GridView 中有不规则数量的项目,例如 3 列 GridView 中有 7 个元素,则此解决方案不起作用。 (2认同)

Fra*_*ank 5

我最终创建了一个自定义 gridview,如下所示:

/sf/answers/683025101/

使用与我的网格视图中的一个项目完全一样高的背景图像,并且底部有一个分隔符。

奇迹般有效!