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

17

我想在GridView的顶部和底部添加空白区域,类似于头部/尾部,但是我只想支持空白,不想支持其他元素。

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

有什么最简单的解决方案吗?谢谢!


将你的gridview放入一个与父布局相匹配宽度和高度的Relative layout中,然后给出topbottom margin - Bills
但是当您滚动网格视图中的项目时,它们无法滚动到顶部/底部边距,对吗?我想在网格视图内留出空间。或者我错过了什么吗? - Lim Thye Chean
2个回答

130

如果我理解你的意思正确,它应该是这样的:

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

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

<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="50dp"
android:paddingBottom="50dp"
android:clipToPadding="false"/>

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

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


这应该就是答案了!太棒了。谢谢! - ppx
4
那个插图非常出色。 - Tom Kidd
非常感谢,那真的帮了很大的忙。 - NaseemH
我一生都在为所有可滚动视图添加页眉和页脚。然后你出现了。请鼓掌。 - VipulKumar
感谢2019年! - Mitulát báti
显示剩余2条评论

3

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

添加

android:layout_marginTop="40dp"
android:layout_marginBottom="40dp"

将内容添加到您的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>

在这个例子中,标题是可见的(红色和带文本),但您可以随时削减与文本属性相关的部分,并将颜色设置为# 0000(透明)。
这是结果: enter image description here enter image description here

谢谢,但我的GridView是通过new GridView(getActivity())生成的,我该如何在应用程序中实现这个? - Lim Thye Chean
你正在添加 **内边距(paddings),应该改为添加外边距(margins)**。我猜你是在使用LayoutParams,并将其应用到你的GridView上。 - Phantômaxx
结果看起来一样 - 图标仍然无法滚动到边缘区域。我正在寻找在GridView中所有图标上方和下方的空间(因此最初第一行图标上方将有空间,然后向上滚动时该区域将被图标占据),目前的结果是图标上方始终会有空间。 - Lim Thye Chean
好的,所以您可以在GridView之前和之后添加自定义标题和页脚行作为TextViews。最好的容器是RelativeLayout-我会更新我的答案,并提供这样的设计。 - Phantômaxx
@VipulKumar 我的解决方案完美地运作了。有什么问题吗?我没有使用动画GIF,这是错的吗? - Phantômaxx
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接