【问题标题】:Add space to top and bottom of GridView在 GridView 的顶部和底部添加空间
【发布时间】:2014-07-05 17:21:29
【问题描述】:

我想在 GridView 的顶部和底部添加空间,类似于页眉/页脚,但我只想支持空格,没有别的。

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

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

【问题讨论】:

  • 将您的gridview 放入与父母的widthheight 匹配的Relative layout,然后给出topbottom margin
  • 但是当您在 gridview 内滚动项目时,它们无法滚动到顶部/底部边距,对吧?我想在gridview中留出空间。还是我错过了什么?

标签: android gridview space


【解决方案1】:

如果我没听错的话,应该是这样的:

Maciej 在这里提到了一个简单的解决方案:How to add extra space inside at the bottom of a 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

【讨论】:

  • 这应该是答案!惊人的。谢谢!
  • 非常感谢您的回答。太棒了:)
  • 那幅插图很棒。
  • 非常感谢,这真的很有帮助。
  • 我一生都在为所有可滚动视图添加页眉和页脚。然后你来了。鞠躬。
【解决方案2】:

不要使用 paddings(在视图 inside 中添加空间),而是使用 ma​​rgins(在 ouside 中添加空间> 你的观点):

添加

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

到您的 GridView(或其他值)

[编辑]

根据 OP 的说明:“空格”必须是 FIXED 并且所有 GridView 都可以滚动。
因此我是这样设计的(通过设置一对锚定的 TextViews 作为固定的页眉和页脚):

<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(透明)

结果如下:

【讨论】:

  • 谢谢,但是我的 GridView 是由 new GridView(getActivity()) 生成的,我如何在应用程序中执行此操作?
  • 您正在添加 填充。改为添加 边距。我想您正在为此使用 LayoutParams,然后将 LayoutParams 应用到您的 GridView。
  • 结果看起来一样 - 图标仍然无法滚动到边缘区域。我正在寻找GridView中所有图标上方和下方的空格(所以最初第一行图标上方会有空格,然后向上滚动时该区域将被图标占据),现在的结果是上面总是有空格图标。
  • 好的,因此您可以在 GridView 之前和之后添加自定义页眉和页脚行作为 TextView。如果容器是一个相对布局更好 - 我会用这样的设计更新我的答案。
  • @VipulKumar 我的解决方案完美运行。那有什么问题?我没有使用动画 GIF?
猜你喜欢
  • 1970-01-01
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-14
  • 2012-07-15
  • 2021-11-23
  • 1970-01-01
相关资源
最近更新 更多