在 Android 中在顶部 ListView 项目上方(以及在最后一个下方)添加边距 [英] Add margin above top ListView item (and below last) in Android

查看:18
本文介绍了在 Android 中在顶部 ListView 项目上方(以及在最后一个下方)添加边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个关于 Android 中 ListView 中项目布局的很好的问题.

This is a pretty fine question about the layout of items in a ListView in Android.

我有一个活动,顶部有一个标题栏,一个 ListView 占据了屏幕的其余部分.我希望我的 ListView 在左侧、右侧和顶部显示 10dp 填充,但是当您向上滚动 ListView 时,我希望它在消失在褪色边缘之前覆盖顶部 10dp 填充.同样,当你滚动到底部时,最后一个列表项应该在最后一个列表项的底部和屏幕的实际底部之间出现 10dp(如果你想知道为什么,那是因为我想要一个漂亮的背景图像在 ListView 周围探出头来).

I have an activity with a title bar at the top and a ListView taking up the rest of the screen. I want my ListView to appear with 10dp padding on the left, right and top, but when you scroll the ListView up, I want it to cover the top 10dp padding before disappearing under the faded edge. Similarly, when you scroll to the bottom, the last list item should appear with 10dp between the bottom of the last list item and the actual bottom of the screen (if you're wondering why, it's because there's a pretty background image that I want poking out around the ListView).

我已经尝试向 ListView 本身添加内边距,但是当您滚动列表时,它会在内边距的边缘下消失.

I've tried adding padding to the ListView itself, but then when you scroll the list it disappears under the edge of the padding.

我来自网络开发背景,类比是在第一个列表项上方(以及最后一个列表项下方)添加边距.

I'm from a web dev background, and the analogy would be to add margin above the first list item (and below the last list item).

推荐答案

您写道:

我已经尝试向 ListView 本身添加内边距,但是当您滚动列表时,它会在内边距的边缘下消失.

I've tried adding padding to the ListView itself, but then when you scroll the list it disappears under the edge of the padding.

ListView 的 clipToPadding 属性设置为 false.这将启用围绕 ListView 滚动到布局末尾(而不仅仅是到填充边缘)的填充.

Set ListView's clipToPadding attribute to false. This will enable padding around the ListView and scrolling to the end of the layout (and not only to the edge of the padding).

示例:

<ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@android:color/transparent"
    android:dividerHeight="10.0sp"
    android:padding="16dip"
    android:clipToPadding="false"/>

android:clipToPaddingViewGroup,布局和视图容器的基类.

android:clipToPadding is an XML attribute of ViewGroup, the base class for layouts and views containers.

相关的方法调用是:

public void setClipToPadding (boolean clipToPadding)

这篇关于在 Android 中在顶部 ListView 项目上方(以及在最后一个下方)添加边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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