在GridView的顶部和底部添加空间

Lim*_*ean 14 android gridview space

我想在GridView的顶部和底部添加空格,类似于页眉/页脚,但我只想支持空格而不是其他任何东西.

目前我正在使用顶部/底部填充,但滚动时,内容将在填充部分中被裁剪.

对此最简单的解决方案是什么?谢谢!

Ele*_*ary 122

如果我理解你的话,它应该是这样的:

为此,Maciej在这里提到了一个简单的解决方案:如何在GridView底部添加额外的空间

您只需在GridView布局中添加以下内容:

<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="50dp"
android:paddingBottom="50dp"
android:clipToPadding="false"/>
Run Code Online (Sandbox Code Playgroud)

最重要的部分是clipToPadding属性,必须将其设置为false.

您还可以查看Google提供的相应博客文章,其中提到了此解决方案:https://plus.google.com/+AndroidDevelopers/posts/LpAA7q4jw9M

  • 这个插图很出色. (3认同)

Fan*_*mas 3

不要使用填充(在视图内部添加空间),而是使用边距(在视图外部添加空间):

添加

android:layout_marginTop="40dp"
android:layout_marginBottom="40dp"
Run Code Online (Sandbox Code Playgroud)

到你的 GridView (或一些不同的值)

[编辑]

根据OP的澄清:“空格”必须是固定的并且所有 GridView 都可以滚动。
因此我这样设计(通过设置一对锚定的 TextView 来充当固定页眉和页脚):

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    android:padding="8dp"
    >
    <!-- Header -->
    <TextView
        android:id="@+id/txtHeader"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_alignParentTop="true"
        android:background="#ff00"
        android:gravity="center"
        android:text="Header"
        android:textColor="@android:color/white"
        android:textSize="24sp"
        android:textStyle="bold"
    />
    <!-- Footer -->
    <TextView
        android:id="@+id/txtFooter"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_alignParentBottom="true"
        android:background="#ff00"
        android:gravity="center"
        android:text="Footer"
        android:textColor="@android:color/white"
        android:textSize="24sp"
        android:textStyle="bold"
    />
    <!-- The Grid -->
<!--    <GridView -->
<!--        android:id="@+id/grdIcons" -->
<!--        android:layout_width="match_parent" -->
<!--        android:layout_height="match_parent" -->
<!--        android:layout_above="@id/txtFooter" -->
<!--        android:layout_below="@id/txtHeader" -->
<!--        android:background="#f0f0" -->
<!--        android:textColor="@android:color/white" -->
<!--        android:textSize="24sp" -->
<!--        android:textStyle="bold" -->
<!--    /> -->
    <GridView
        android:id="@+id/grdIcons"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/txtFooter"
        android:layout_below="@id/txtHeader"
        android:background="#f00f"
        android:textColor="@android:color/white"
        android:textSize="24sp"
        android:textStyle="bold"

        android:columnWidth="64dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="8dp"
        android:horizontalSpacing="8dp"
        android:stretchMode="none"
        android:gravity="center"
    />

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

在此示例中,标题是可见的(红色且带有文本),但您始终可以剪切与文本属性相关的部分并将颜色设置为#0000(透明)

结果如下:

在此输入图像描述 在此输入图像描述