【发布时间】:2013-07-29 00:48:37
【问题描述】:
我需要添加以添加具有复杂项目背景的 ListView:顶部和底部的偶数/奇数和圆角不同。它看起来像这样:
我已经通过 level-list 实现了所有这些东西,但我还想做一件事。 现在底部项目靠近屏幕底部。最好增加一些空间。
我不想为 ListView 添加底部边距,我只需要最后一项的边距。
我看到的方法:
页脚
一种技巧——将带有空 TextView 的页脚添加到 ListView。但是页脚是很不稳定的东西,通常在 notifyDataSetChanged 后它们会消失,并且无法恢复
具有透明像素的图像
我要求设计师将透明像素添加到底部背景资源。不幸的是,在这种情况下,垂直居中完全被破坏了。 比如有9个补丁是这样的:
这样的布局:
<?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 的背景,则居中效果很好。如果您知道任何文章,对此进行解释,请告诉我。)
在适配器实现中为最后一项添加底部边距
这应该可以,但是由于未知原因,我仍然无法使其正常工作。 我不喜欢这种方式,因为我不喜欢在代码中修改尺寸。
所以
已经有一种想象的方式——用特定的位图和边距构造一些 XML 可绘制对象。根据drawables概念应该是可能的,但我找不到实现。有人知道吗?
还有其他想法吗?
【问题讨论】:
-
您只想在最后一项后添加边距?
-
是的。这个问题听起来一模一样
-
请在下方查看我的回答
-
我实际上会考虑使用页脚方法。我从未见过它们自己消失,尤其是在致电
notifyDataSetChanged()之后。如果发生这种情况,您可能做错了其他事情。页脚由ListView管理,与适配器的实际数据集几乎没有关系。 -
最好的方法是 [stackoverflow.com/questions/6288167/… [1]: stackoverflow.com/questions/6288167/… 它真的帮助了我
标签: android centering android-drawable