首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >向ListView最后一个元素添加下边距

向ListView最后一个元素添加下边距
EN

Stack Overflow用户
提问于 2013-07-29 17:32:24
回答 6查看 27.1K关注 0票数 84

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

我已经通过level-list实现了所有这些东西,但我还有一件事要做。现在底部的项目接近屏幕的底部。最好是添加一些空间。

我不想添加底部边距到ListView,我只需要最后一项的边距。

我看到了这样做的方法:

页脚

一种向ListView添加带有空TextView的附加页脚的方式。但是页脚是非常不稳定的东西,它们通常在notifyDataSetChanged之后消失,并且没有办法找回它们

具有透明像素的图像

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

布局是这样的:

<?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>

结果是:

如您所见,居中不起作用。不幸的是。(顺便说一句,如果指定这个9补丁作为TextView的背景,居中效果很好。如果您知道任何文章,解释这一点,请让我知道。)

将下边距添加到适配器实现中的最后一项

这应该可以工作,但由于未知的原因,我仍然不能让它工作。我不喜欢这种方式,因为我不喜欢修改代码中的维度。

所以

已经有了一种想象的方法--构造一些具有特定位图和边距的可绘制的XML。根据drawables的概念,这应该是可能的,但我找不到实现。也许有人知道?

还有其他想法吗?

EN

回答 6

Stack Overflow用户

发布于 2015-10-27 02:15:16

在列表中添加一个空的页脚,如下所示:

TextView empty = new TextView(this);
empty.setHeight(150);
listview.addFooterView(empty);
票数 8
EN

Stack Overflow用户

发布于 2015-02-23 14:05:14

如果你愿意,你也可以从代码中这样做,例如,我在这里对EditText不同的情况做出反应:

   if(s.toString().length()>0)
   {
      contacts_lv.setClipToPadding(false);
      contacts_lv.setPadding(0,0,0,270*screenDensity);
   }
   else
   {
      contacts_lv.setClipToPadding(true);
      contacts_lv.setPadding(0,0,0,0);
   }
票数 4
EN

Stack Overflow用户

发布于 2015-02-24 09:54:12

钟表匠的回答是最好的,也是相当聪明的。您还可以创建一个空的页脚视图。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17920492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档