【发布时间】:2015-05-27 22:11:21
【问题描述】:
我正在尝试向ListView 添加自定义空视图,如this Q&A 所示。但是ListView 似乎将空视图添加到其父级,或者可能添加到根ViewGroup,而不是添加到自身。我在这里做错了吗?
我创建了一个小布局文件来演示该问题。注意空视图是如何放置在活动的左上角的,完全在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>
【问题讨论】:
-
“我在这里做错了吗?” -- 好吧,您可能希望将
TextView居中。这由您应用的布局规则控制,现在,您将其设置在左上角。 -
@CommonsWare 同意。但最直接的问题是它在哪个视图的左上角。
-
嗯,它必须在某个地方。它不能是
ListView的孩子。如果你不喜欢它的位置,把它移到别处。你还没有真正解释问题是什么。 -
@CommonsWare 列表项不是
ListView的子项吗?如果不是,那么问题是我假设它们是并且空视图也应该是。如果那是错的,那什么是对的?
标签: android android-listview android-5.0-lollipop