首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >当ListView为空时显示空视图

当ListView为空时显示空视图
EN

Stack Overflow用户
提问于 2010-09-23 00:40:11
回答 9查看 163K关注 0票数 139

由于某些原因,即使ListView不为空,空视图(在本例中为TextView )也会始终出现。我认为ListView会自动检测何时显示空视图。

<RelativeLayout android:id="@+id/LinearLayoutAR"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">
    <ListView android:id="@+id/ARListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"></ListView>
    <ProgressBar android:id="@+id/arProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"></ProgressBar>
    <!-- Here is the view to show if the list is emtpy -->
    <TextView android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="No Results" />
</RelativeLayout>

如何正确地连接空视图?

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2010-09-23 00:48:15

应该是这样的:

<TextView android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="No Results" />

请注意id属性。

票数 136
EN

Stack Overflow用户

发布于 2012-04-14 07:24:23

正如appsthatmatter所说,在布局中类似于:

<ListView android:id="@+id/listView" ... />
<TextView android:id="@+id/emptyElement" ... />

在链接的活动中:

this.listView = (ListView) findViewById(R.id.listView);
this.listView.setEmptyView(findViewById(R.id.emptyElement));

也适用于GridView..。

票数 119
EN

Stack Overflow用户

发布于 2014-01-02 15:32:20

我强烈建议您像这样使用ViewStubs

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <ViewStub
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout="@layout/empty" />
</FrameLayout>

查看来自Cyril Mottier的完整示例

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

https://stackoverflow.com/questions/3771568

复制
相关文章

相似问题

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