我曾经多次使用自定义布局创建带有标题和行项目的列表视图,但总是会让我恼火的是Android Studio中的UI预览不能显示预览。显然,这是因为ListView或CursorAdapter通过编程方式加载自定义布局,但如果我想以某种方式在xml中指定标题和页脚布局,以便查看预览怎么办?有人知道如何做吗?
我曾经多次使用自定义布局创建带有标题和行项目的列表视图,但总是会让我恼火的是Android Studio中的UI预览不能显示预览。显然,这是因为ListView或CursorAdapter通过编程方式加载自定义布局,但如果我想以某种方式在xml中指定标题和页脚布局,以便查看预览怎么办?有人知道如何做吗?
您可以使用tools:listitem
。只需在布局的根中添加tools命名空间即可。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
(...)
接着在你的ListView中设置列表项的布局
<ListView
android:id="@+id/mylistView"
tools:listitem="@layout/my_list_item"
(...)
还可以使用listheader
/listfooter
来设置页眉/页脚。
xmlns:tools="http://schemas.android.com/tools"
命名空间用于开发目的(预览),不会编译进应用程序中。
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listfooter="@layout/item_footer"
tools:listheader="@layout/item_header"
tools:listitem="@layout/item" />