Android ListView 填充内容而不移动 ListView 高度?
我有一个 ListView,里面有一堆项目。如何使顶部和底部项目的顶部边距为 10dp,底部项目的底部边距为 10dp?现在我可以通过 ListView 上的填充或边距来做到这一点,但结果是,当您滚动时,ListView 的边缘现在距屏幕底部 10dp。无论如何?我还尝试在适配器的 getView 方法内部设置边距,但我没有看到 AbsListView.LayoutParams 的任何边距选项。任何帮助都会很棒。
谢谢
I have a ListView with a bunch of items inside it. How do I make the top and bottom items have a top margin of 10dp for top item and bottom margin of 10dp for bottom item? Now I can do this with padding or margin on the ListView but the result is that when you scroll the edge of the ListView is now 10dp from the bottom of the screen. Anyway around this? I have also tried setting margins inside of the getView method of my adapter but I don't see any margin options for AbsListView.LayoutParams. Any help would be great.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
诀窍是在视图定义中包含
android:clipToPadding="false"
,例如:The trick is to include
android:clipToPadding="false"
in your view definition, e.g.:您可以通过使用页眉和页脚在滚动区域内的 ListView 顶部和底部获得空间。这是一个简单的示例:
定义您的填充视图,我将其命名为 header_footer.xml:
现在,您的
onCreate
方法如下所示,假设您将 ListView 命名为listView
:然后设置您的适配器。
You can get space at the top and bottom of your ListView, inside the scrolling region, by using headers and footers. Here's a quick example:
Define your filler view, which I named header_footer.xml:
Now your
onCreate
method looks like this, assuming you named your ListViewlistView
:And then set your adapter.
为什么不只检查项目相对于列表大小的位置:
填充:
对于边距,请使用
示例:
前提是您已为适配器正确实现了 getCount() 方法。
Why not just check the position of the item relative to the size of your list:
Padding:
For margins use
Ex:
this is provided you've implemented the getCount() method properly for your adapter.