【发布时间】:2015-07-07 22:13:39
【问题描述】:
当我将 RecyclerView 添加到布局时,它显示为空白屏幕。有没有办法,比如通过tools命名空间,显示RecyclerView的内容预览?
【问题讨论】:
标签: android android-studio android-recyclerview android-tools-namespace
当我将 RecyclerView 添加到布局时,它显示为空白屏幕。有没有办法,比如通过tools命名空间,显示RecyclerView的内容预览?
【问题讨论】:
标签: android android-studio android-recyclerview android-tools-namespace
从 Android Studio 1.3.1 开始,它会在预览中显示默认列表项,但还不允许您指定自己的列表项。希望它会来。
【讨论】:
@oRRs 是对的!
我使用的是 Android Studio 1.4 RC2,您现在可以指定任何自定义布局。
我尝试了一个自定义 CardView,它可以工作。
tools:listitem="@android:layout/simple_list_item_checked"
【讨论】:
tools:orientation="horizontal"
tools:orientation="horizontal"或android:orientation="horizontal"之外,我还必须按照stackoverflow.com/questions/35681433/…指定app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
Android 工具和 LayoutManager
tools命名空间启用设计时功能(例如在片段中显示哪种布局)或编译时行为(例如将哪种收缩模式应用于您的 XML 资源) 这是一个非常强大的功能,正在开发中,让您不必每次都编译代码来查看更改
AndroidX[About] 和 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/* resources 轻松可视化您的布局结构
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代码中!
首先,在您的项目 XML 中添加以下行,以便在您编辑项目时预览您的列表:
tools: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>
【讨论】: