向ListView的最后一个元素添加下边距 [英] Adding bottom margin to ListView last element

查看:148
本文介绍了向ListView的最后一个元素添加下边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要添加以添加具有复杂项目背景的ListView:顶部和底部的偶数/奇数和圆角有所不同.看起来像这样:

I need to add to add ListView with complicated items background: different for even/odd and rounded corners at the top and bottom. It looks like this:

我已经通过关卡列表实现了所有这些东西,但是我还想做一件事. 现在,底部项目位于屏幕底部附近.最好增加一些空间.

I have implemented all this stuff via level-list, but there is one more thing I want to do. Now the bottom item is near the bottom of the screen. It is better to add some space.

我不想在ListView中添加底边距,我只需要为最后一项添加边距.

I don't want to add bottom margin to ListView, I need margin only for last item.

我看到的方式:

一种技巧–将带有空白TextView的页脚添加到ListView.但是页脚是相当不稳定的东西,它们通常在notifyDataSetChanged之后消失,并且没有办法使它们恢复原状

A kind of hack – add footer with empty TextView to ListView. But footers are quite unstable things, they usually disappear after notifyDataSetChanged and there is no way to get them back

我要求设计师将透明像素添加到底部背景资源.不幸的是,在这种情况下,垂直居中被完全破坏了. 例如,有9patch这样的:

I asked designer to add transparent pixels to bottom background resource. Unfortunately, in this case vertical centering is completely broken. For example, there is 9patch like this:

布局如下:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
        >
    <!-- View with background with transparent pixels on bottom -->
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
                  android:id="@+id/item"
                  android:background="@drawable/some_bgr"
                  android:padding="10dp"
            >
        <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"
                  android:text="Title"
                  android:layout_gravity="center"
                  android:textSize="18sp"
                />
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                  android:text="Detail"
                  android:layout_gravity="center"
                  android:textSize="18sp"
                />
    </LinearLayout>

    <!-- Just for marking place took by view -->
    <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"
                 android:layout_below="@id/item"
                 android:background="#88ff55"
            />
</RelativeLayout>

结果:

如您所见,居中不起作用.很遗憾. (顺便说一句,如果将此9patch指定为TextView的背景,则居中效果很好.如果您知道任何文章,对此进行了说明,请告诉我.)

As you see, centering is not working. Unfortunately. (BTW, if specify this 9patch as background for TextView, centering works good. If you know any article, explaining this, please let me know.)

那应该可以,但是由于未知的原因,我仍然无法使它起作用. 我不喜欢这种方式,因为我不喜欢在代码中修改尺寸.

That should work, but for unknown reason I still can't get it work. I don't like this way, because I don't like to modify dimensions in code.

已经有一种虚构的方法–构造一些具有特定位图和边距的XML可绘制图形.根据drawables概念,这应该是可能的,但我找不到实现方法.可能有人知道吗?

There is already imaginary way – construct some XML drawable with particular bitmap and margin. According to drawables concept it should be possible, but I can't find implementation. May be somebody knows?

还有其他想法吗?

推荐答案

在您的ListView中,设置paddingBottomclipToPadding="false".

  <ListView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="8dp"
      android:clipToPadding="false"
      android:scrollbarStyle="outsideOverlay"/>

  • 这也适用于RecyclerView.

    如果希望滚动条不溢出到填充区域中,请仅使用android:scrollbarStyle="outsideOverlay".

    Only use android:scrollbarStyle="outsideOverlay" if you want the scroll bar to not overflow into the padded area.

    这篇关于向ListView的最后一个元素添加下边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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