【问题标题】:Android empty list layoutAndroid空列表布局
【发布时间】:2009-11-18 13:39:37
【问题描述】:

Android 空列表布局

无论我做什么,我都无法显示空标签。在我的标签中,我有:

<LinearLayout
android:id="@+id/thisParticularTab"
android:orientation="vertical"
android:layout_width="fill_parent" 
android:layout_height="fill_parent">

<TextView android:id="@+id/LastUpdated"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:text="List last updated: N/A" />

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

<Button id="@android:id/empty"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:text="Click to fill the list" /></LinearLayout>

但是,尽管我的 ListView 为空(未显示任何项目,并且正在读取的数据库表为空),但未显示该按钮。有什么想法吗?

另外,当它出现时,我可以引用 R.id.empty 吗?这真的意味着我每个活动只能有 1 个 ListView/empty 吗?我的理解是“id”用于定义 UI 定义;我怎样才能给它一个名字,并将它分配为 XML 中的空视图? (我不想使用 setEmptyView())

【问题讨论】:

  • 您需要使用 ListActivity 的实例才能使其工作 - @android:id/empty。否则,您需要创建一个具有常规 id 的自定义空视图 - @+id/empty。

标签: android user-interface listview


【解决方案1】:

你在这里错过的是按钮 id 也需要 android 前缀:

<Button 
   android:id="@android:id/empty"
   ... />

@android:id/empty 指的是android.R.id.empty,而不是R.id.empty

确实,每个视图应该只有一个给定 id 的元素。如果您正在创建自己的列表视图项,您自然会一遍又一遍地拥有相同的元素(以及随后相同的 ID),但在这些情况下,您可以通过 myListItem.findViewById(id) 访问它们

与您的场景更相关,如果您发现需要添加另一个列表视图,它需要另一个空标签,并且仍然希望列表视图固有地处理该项目的隐藏和显示,您可以使用函数 @987654326 @

【讨论】:

  • 据我了解,“android:id/empty”id 标签会在 XML 中自动执行此操作;还是我还需要检查 ListView 是否为空?
  • 你说得对,我第一次误读了你的代码。查看我的编辑
  • 谢谢!不幸的是,这也不起作用;我想知道是不是因为它太“深”了()。使用 setEmptyView(View) 函数可以完美地工作 - 我想它看起来并不漂亮,但可以发货:)
【解决方案2】:

试试:

<Button id="@id/android:empty"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:text="Click to fill the list" />

然后找到按钮:

 Button empty = (Button) findViewById(android.R.id.empty);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多