如何设置空ListView的背景图像?

dav*_*vid 6 android listview background image

如何设置listview背景像这样.我想在记录数0的时候出现

在此输入图像描述

Ser*_*tov 14

有特殊方法ListView- setEmptyView().您可以在此处此处找到使用它的示例.

更新:第二个链接现在不可用.这是文章的引用:

当您以编程方式设置ListView的"空视图"时,您最终可能会在列表为空时为什么不显示空视图.
如果发生这种情况,那么您忘记了必须手动将空视图添加到视图层次结构中,因为ListView不会为您执行此操作.虽然很明显,当你考虑它时,文档没有提到这个细节,谷歌搜索显示至少有一个人有这个问题.

这是带有线条的代码,在数字4和5处很容易忘记...

TextView emptyView = new TextView(context);
emptyView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
emptyView.setText(“This appears when the list is empty”);
emptyView.setVisibility(View.GONE);
((ViewGroup)list.getParent()).addView(emptyView);
list.setEmptyView(emptyView);
Run Code Online (Sandbox Code Playgroud)


Fra*_*nke 7

只需在父布局中设置背景图像,然后将ListView的颜色设置为完全透明:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    style="@style/Main" android:background="@drawable/background">
    <ListView android:cacheColorHint="#00000000" .../>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)