简体   繁体   English

在 Android 中的顶部 ListView 项目上方(和最后一个下方)添加边距

[英]Add margin above top ListView item (and below last) in Android

This is a pretty fine question about the layout of items in a ListView in Android.这是一个关于 Android 中 ListView 中项目布局的非常好的问题。

I have an activity with a title bar at the top and a ListView taking up the rest of the screen.我有一个活动,顶部有一个标题栏,一个 ListView 占据了屏幕的 rest。 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.我希望我的 ListView 在左侧、右侧和顶部显示 10dp 填充,但是当您向上滚动 ListView 时,我希望它在消失在褪色边缘之前覆盖顶部 10dp 填充。 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).同样,当您滚动到底部时,最后一个列表项应该在最后一个列表项的底部和屏幕的实际底部之间以 10dp 出现(如果您想知道为什么,那是因为我想要一个漂亮的背景图像在 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 本身添加填充,但是当您滚动列表时,它会在填充边缘下消失。

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).我来自 web 开发背景,类比是在第一个列表项上方(以及最后一个列表项下方)添加边距。

You wrote:你写了:

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 本身添加填充,但是当您滚动列表时,它会在填充边缘下消失。

Set ListView's clipToPadding attribute to false .ListView's clipToPadding属性设置为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周围的填充滚动到布局的末尾(而不仅仅是填充的边缘)。

An example:一个例子:

<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:clipToPadding is an XML attribute of ViewGroup , the base class for layouts and views containers. android:clipToPaddingViewGroup的 XML 属性,是布局和视图容器的基础 class 属性。

The related method call is:相关的方法调用是:

public void setClipToPadding (boolean clipToPadding)
Space padding = new Space(this);
padding.setHeight(20); // Can only specify in pixels unfortunately. No DIP :-(

ListView myListView = (ListView) findViewById(R.id.my_list_view);

myListView.addHeaderView(padding);
myListView.addFooterView(padding);

myListView.setAdapter(myAdapter);

The above ListView will have a header and footer padding of 20 pixels.上面的 ListView 将有一个 header 和 20 像素的页脚填充。

Appendage to @Jakobud's answer... @Jakobud 回答的附录……

My listView was already making use of the android:divider/android:dividerHeight properties to create transparent gaps between listView items.我的 listView 已经在使用android:divider/android:dividerHeight属性在 listView 项目之间创建透明间隙。 This allowed me to simply add the android:headerDividersEnabled and android:footerDividersEnabled properties and set the Header and Footer views to new View(Activity.this) .这使我可以简单地添加android:headerDividersEnabledandroid:footerDividersEnabled属性并将 Header 和页脚视图设置为new View(Activity.this)

Slight simplification for cases where you already have dividers setup in the listView.对于您已经在 listView 中设置分隔符的情况,稍微简化一下。

My solution using a ListFragment , based on the solutions by @Jakobud and @greg7gkb.我使用ListFragment的解决方案,基于@Jakobud 和@greg7gkb 的解决方案。

ListView listView = getListView();
listView.setDivider(null);
listView.setDividerHeight(getResources().getDimensionPixelSize(R.dimen.divider_height));
listView.setHeaderDividersEnabled(true);
listView.setFooterDividersEnabled(true);
View padding = new View(getActivity());
listView.addHeaderView(padding);
listView.addFooterView(padding);

@Gunnar Karlsson's answer is good, but has an issue of cell views being recycled prematurely when completely behind the padding, but not yet fully off the screen. @Gunnar Karlsson 的回答很好,但是当完全在填充后面但还没有完全离开屏幕时,单元格视图会被过早回收。 Setting clipToPadding=false is responsible for this and may or may not be fixed in a future version of android.( When using clipToPadding in ListView's the items get recycled prematurely )设置 clipToPadding=false 对此负责,并且可能会或可能不会在 android 的未来版本中修复。( 在 ListView 中使用 clipToPadding 时,项目会过早回收

I have a nice simple solution with no side effects:我有一个很好的简单解决方案,没有副作用:

  1. Add an outer (Linear or Relative) Layout to your cell_design.xml将外部(线性或相对)布局添加到您的 cell_design.xml
  2. On this outer Layout add padding (ie 10dip) to create a "margin" around the whole cell.在这个外部布局上添加填充(即 10dip)以在整个单元格周围创建一个“边距”。 (NB only padding will work, not margin on the outer layout) (注意只有 padding 可以工作,而不是外部布局的 margin)
  3. On the ListView set android:dividerHeight="-10dip" , the opposite of what is around the cell在 ListView 设置android:dividerHeight="-10dip"上,与单元格周围的相反

Compared to the other answer, there is no need to set the divider colour.与其他答案相比,无需设置分隔线颜色。 The padding at the topmost and bottommost cells will be present, and the negative divider will prevent double height dividers in between.将存在最顶部和最底部单元格的填充,负分隔符将防止其间出现双倍高度分隔符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM