ListView 将空视图添加到 root/parent 而不是自身?

ListView adds empty view to root/parent instead of self?

我正在尝试将自定义空视图添加到 ListView,如 this Q&A 中所示。但是 ListView 似乎将空视图添加到它的父视图,或者可能添加到根 ViewGroup,而不是它自己。我是不是做错了什么?

我创建了一个小的布局文件来演示这个问题。注意空视图如何放置在 activity 的左上角,完全在 ListView:

之外
<?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" >

    <TextView
        android:id="@+id/top_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Some text above the ListView" />

    <ListView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/top_text" />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ListView empty text" />

</RelativeLayout>

Aren't the list items children of the ListView?

是的,当 ListView 通过关联的 ListAdapter 创建它们时。

空视图不是列表项。它不是 ListView 的子项。这只是另一种观点。 AdapterView 切换自身和空视图的可见性,具体取决于 AdapterView 是否为空。如果 AdapterView 为空,则其自身的可见性设置为 GONE,空视图设置为 VISIBLE。如果 AdapterView 不为空,则其自身的可见性设置为 VISIBLE,空视图的可见性设置为 GONE。请参阅 the source code for AdapterView 中的 updateEmptyStatus()

鉴于可见性切换效果,典型的方法是让空视图与 ListView 大致占据相同的 space。但是,这不是必需的。