Android:将空视图设置为列表视图 [英] Android: set empty view to a list view

查看:22
本文介绍了Android:将空视图设置为列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <TextView
            android:id="@android:id/empty"
            style="@style/FacebookFont"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/no_result_found"
            android:textColor="@color/white" />

这是空视图的xml代码.我设置空视图如下:

this is the xml code for empty view. I am setting empty view as follows:

    list.setAdapter(adapter);
    list.setEmptyView(findViewById(android.R.id.empty));

即使在填充列表视图之前,我在列表视图中也有项目显示空视图.当列表视图包含某些项目时,我不希望显示该空视图.

I have the items in the listview even then also before listview is populated the empty view is shown. I don't want that empty view to be shown when listview contains some item.

请帮忙.

推荐答案

如果您使用的是 ListActivity 那么无需手动设置空视图.从您的代码中删除 list.setEmptyView(findViewById(android.R.id.empty));.

if you are using ListActivity then no need to set the empty view manually. Remove list.setEmptyView(findViewById(android.R.id.empty)); from your code.

像下面一样改变你的布局,如果列表没有项目,它会自动添加EmptyView:

Change your layout like below then it's automatically add EmptyView if list has no item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="8dp"
        android:paddingRight="8dp">

    <ListView android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#00FF00"
            android:layout_weight="1"
            android:drawSelectorOnTop="false"/>

     <TextView android:id="@android:id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF0000"
            android:text="No data"/>

</LinearLayout>

这篇关于Android:将空视图设置为列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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