如何在GridView底部添加额外的空间

13
我需要帮助在GridView底部添加一个空白间隔。这个间隔应该在最后一个GridView元素下面,而不是作为下一个元素的边距。只有当用户滚动到GridView底部时,才应该显示此间隔。原因是广告横幅部分覆盖了GridView底部。除此障碍外,用户仍然应该能够看到GridView的全部内容,这就是为什么需要在GridView底部添加空白间隔的原因。
图片描述:左侧:广告(蓝色)覆盖了GridView元素的一部分(橙色);右侧:广告覆盖了GridView底部的空白间隔

same approach but with space at the bottom

例如,它将会是什么样子,只需想象一下空间在底部而不是顶部。

到目前为止,我尝试使用Padding和Margin变量来解决底部的问题,但它们并不是正确的变量。我还在stackoverflow上搜索了一些类似的问题,比如:Add extra space to bottom of a GridView or ListView或者:Adding a footer View to a multi-column GridView in Android?。但这些解决方案似乎不适用于我的情况,而且我正在寻找一个在布局文件中而不是源代码中的解决方案(如果有的话)。

非常感谢您的帮助。


设置gridview的bottomPadding。 - Meenal
您可以尝试在底部插入一个空的视图元素。 - Shishir Gupta
我需要在GridView底部的空间,而不是在GridView下方。这就是为什么上面两种方法对我都不起作用的原因。 - Elementary
5个回答

30
为了实现你想要的效果,你需要为你的GridView添加底部填充,并且关闭clipToPadding行为。尝试使用以下XML代码:

为了实现您所需的效果,您需要向GridView添加底部填充,并禁用clipToPadding行为。请尝试使用以下XML代码:

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

如果您希望的话,也可以通过代码来完成。好处是您可以在代码中动态计算偏移量:

gridView.setPadding(int left, int top, int right, int bottom);
gridView.setClipToPadding(false);

注意: 如果不禁用clipToPadding行为,您的GridView底部将会有持续的空白区域,因此禁用它非常重要。


额外奖励: 这里还有一个关于在ListViewGridView中使用clipToPadding参数的好链接:https://plus.google.com/+AndroidDevelopers/posts/LpAA7q4jw9M


很遗憾,这对我不起作用。在 GridView 底部仍然没有额外的空间。在滚动到底部后,覆盖广告仍然会阻挡 GridView 的内容。 - Elementary
1
这必须能够工作...请查看我编辑的答案中发布的链接。你一定做错了什么。我在我的许多应用程序中都在使用它。也许你只需要设置更大的填充或者你在某个地方(例如代码中)覆盖了它。 - Maciej Ciemięga
根据博客文章,这种方法似乎是正确的,所以我现在正在搜索我的代码为什么不起作用。 :) - Elementary
1
我找到了我的错误,谢谢你的解决方案,它对我非常有效。 :) - Elementary
没问题:) 顺便问一下,错误是什么?这可能对其他人有帮助,他们将来可能会遇到类似的问题。 - Maciej Ciemięga
显示剩余6条评论

0
要获得以下结果 带有按钮上方空间的GridView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_submit"
        style="@style/Base.Widget.AppCompat.Button.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/gv_someGrid"
        android:layout_margin="10dp"
        android:layout_alignParentBottom="true"
        android:text="submit" />

    <GridView
        android:id="@+id/gv_someGrid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/btn_submit" />
</RelativeLayout>

关键是在按钮底部声明Gridview,然后添加约束布局_above = "@+id/your_id"。

0

目前我所拥有的是:

在你的Activity中:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid);

        final TextView txt = (TextView) findViewById(R.id.textView1);
        txt.setVisibility(View.GONE);

        final GridView grid = (GridView) findViewById(R.id.gridViewCustom);


        grid.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                if((firstVisibleItem + visibleItemCount) == totalItemCount){//it means you are at the end of the gridview
                    txt.setVisibility(View.VISIBLE);
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 50);
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
                    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
                    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);

                    RelativeLayout.LayoutParams paramsGrid = (RelativeLayout.LayoutParams) grid.getLayoutParams();
                    paramsGrid.addRule(RelativeLayout.ABOVE, txt.getId());

                    txt.setLayoutParams(params);
                    grid.setLayoutParams(paramsGrid);

                }
            }
        });
    }

.xml

<RelativeLayout 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/gridViewCustom"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_above="@+id/textView1"
       android:layout_alignParentRight="true"
       android:layout_margin="4dp"
       android:columnWidth="80dp"
       android:gravity="center"
       android:numColumns="auto_fit"
       android:stretchMode="columnWidth" >
   </GridView>

   <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="50dp"
       android:layout_alignParentBottom="true"
       android:layout_alignParentLeft="true"
       android:layout_alignParentRight="true"
       android:text=""
       android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

我看到这是一个可能的解决方案,但首先我想看看能否让@Maciej的解决方案起作用。 - Elementary
我采用了Maciej的解决方案,因为你只需要在网格布局中添加两个参数,就可以完成。不需要任何变通方法和额外的源代码。 - Elementary

0

制作自定义网格视图,在getview()方法中,使用视图来创建所需的空间。


如果可能的话,我正在寻找现有的GridView更简单的解决方案。 - Elementary
没有自定义的GridView是不可能的。你至少应该付出一些努力 :) - Pankaj
当然,我已经付出了努力,并将继续努力。当然,在最后你总是可以制作自定义控件。但只要现有的控件存在更简单的解决方案,为什么我要这样做呢? - Elementary
就像我说的,你可以自定义所有内容,但很多时候这种努力带来的痛苦大于收益,因为通常存在更简单的解决方法。这个问题只有几分钟的历史,为什么不再等一会儿,看看其他用户是否知道答案呢?;) - Elementary
1
你好Pankaj,Maciej的解决方案对我来说完全有效。这不需要更改自定义控件或代码,只需在布局中添加两个参数即可。 :) - Elementary
显示剩余2条评论

-3
<GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:marginBottom="50dp"
    android:clipToPadding="false"/>

这不是一个有效的解决方案... margin 会将 GridView 上移,使得 GridView 下方出现持久的空白空间。 - Maciej Ciemięga

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