如何在 GridView 底部添加额外的空间 [英] How to add extra space inside at the bottom of a GridView

查看:32
本文介绍了如何在 GridView 底部添加额外的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助在 GridView 底部添加一个空格.这个空间应该在 GridView 的最后一个元素之下,在它里面.这个空间不应该像下一个元素的边距一样工作,它应该只在用户滚动到 GridView 底部时可见.其原因是广告横幅部分覆盖​​了 GridView 的底部.除了这个障碍,用户应该仍然能够看到 GridView 的全部内容,这就是为什么需要 GridView 底部的空间.

I need help to add a space inside a GridView at the bottom of it. This Space should be below the last Element of the GridView, inside of it. This space shouldn't work like a margin to the next element, it should be only visible, when the user scrolles to the bottom of the GridView. The reason for this is an ad banner which partly covers the bottom of the GridView. Besides this obstruction, the user should still be able to see the whole content of the GridView, that's why the space at the bottom of the GridView is needed.

left:广告(蓝色)覆盖了部分 GridView 元素(橙色);右:广告覆盖了 GridView 底部的空间

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

到目前为止,我尝试使用底部的填充和边距变量,但它们不是解决问题的正确变量.我还搜索了stackoverflow,发现了一些类似的问题,例如:在 GridView 或 ListView 底部添加额外空间 或:在 Android 中向多列 GridView 添加页脚视图?.但是这些解决方案似乎不适合我的情况,而且我正在布局文件中而不是在源代码中(如果有的话)寻找解决方案.

So far I tried to work with the Padding and Marging Variables for Bottom, but they are not the right variables for the problem. I also searched through stackoverflow, I found some similar questions like: Add extra space to bottom of a GridView or ListView or: Adding a footer View to a multi-column GridView in Android?. But the solutions doesn't seem to fit my case and furthermore I am searching for a solution inside the layout file and not inside the source code (if there is any of course).

非常感谢您的帮助.

推荐答案

要实现您想要的效果,您需要在 GridView 中添加底部填充,同时禁用 clipToPadding行为.试试下面的 XML 代码:

To achieve that you want you need to add a bottom padding to your GridView but also disable clipToPadding behavior. Try the XML code below:

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

如果需要,您也可以从代码中执行此操作.好处是您可以在代码中动态计算偏移量:

You can also do it from code if you want. The benefit is that you can compute the offset dynamically in code:

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

注意:如果不禁用 clipToPadding 行为,您将最终在 GridView 底部留下持久的空白区域,因此禁用它非常重要.

Note: Without disabling the clipToPadding behavior you will end up with persistent empty area on the bottom of your GridView, so disabling it is very important.

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

Bonus: Here is also a nice link about using clipToPadding parameter in ListView or GridView: https://plus.google.com/+AndroidDevelopers/posts/LpAA7q4jw9M

这篇关于如何在 GridView 底部添加额外的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆