当我将RecyclerView添加到布局中时,它会显示为空白屏幕。是否有一种方式,如通过tools
命名空间,在RecyclerView内容的预览中显示?
当我将RecyclerView添加到布局中时,它会显示为空白屏幕。是否有一种方式,如通过tools
命名空间,在RecyclerView内容的预览中显示?
@oRRs是正确的!
我正在使用Android Studio 1.4 RC2,现在你可以指定任何自定义布局。
我尝试了一个自定义的CardView,它能够工作。
tools:listitem="@android:layout/simple_list_item_checked"
Android 工具和布局管理器
tools
命名空间可以启用设计时功能(例如在片段中显示哪个布局)或编译时行为(例如将哪种缩小模式应用于您的 XML 资源)。这是一个非常强大的特性,它正在不断发展,允许您不必每次编译代码就可以查看更改。
AndroidX[关于] 和 GridLayoutManager
implementation 'androidx.recyclerview:recyclerview:1.1.0'
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
tools:listitem="@layout/item"
tools:itemCount="10"
tools:orientation="vertical"
tools:scrollbars="vertical"
tools:spanCount="3"/>
支持库和LinearLayoutManager
implementation 'com.android.support:recyclerview-v7:28.0.0'
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layoutManager="android.support.v7.widget.LinearLayoutManager"
tools:listitem="@layout/item"
tools:itemCount="3"
tools:orientation="horizontal"
tools:scrollbars="horizontal" />
在Android Studio 3.0中引入的另一个很酷的功能是通过工具属性预定义数据,使用@tools:sample/*
资源来轻松可视化您的布局结构。
item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="10dp"
android:orientation="vertical"
tools:background="@tools:sample/backgrounds/scenic">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorWhite"
tools:text="@tools:sample/first_names" />
</FrameLayout>
模拟器结果:
listitem
选项,我可以直接在XML代码中输入它! - George Udosentools:showIn="@layout/activity_my_recyclerview_item"
接下来,在RecyclerView的XML文件中添加以下代码,以预览列表中的项目外观:
tools:listitem="@layout/adapter_item"
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
...
...
**tools:listitem="@layout/name_of_your_custom_item_view"**
...>
</androidx.recyclerview.widget.RecyclerView>
从Android Studio 1.3.1开始,在预览中显示默认列表项,但目前还不能指定自己的列表项。希望将来会实现。
tools:orientation="horizontal"
。 - android developertools:orientation="horizontal"
或android:orientation="horizontal"
外,根据 https://dev59.com/uVsV5IYBdhLWcg3w4iFA,我还需要指定app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
。 - Michael Osofsky