当我将RecyclerView添加到布局中时,它显示为一个空白屏幕。是否有一种方法(例如通过工具名称空间)来显示RecyclerView内容的预览?
当前回答
@oRRs是对的!
我使用Android Studio 1.4 RC2,你现在可以指定任何自定义布局。
我尝试了一个自定义CardView,它工作。
tools:listitem="@android:layout/simple_list_item_checked"
其他回答
从Android Studio 1.3.1开始,它会在预览中显示默认的列表项,但还不能让你自己指定。希望它会到来。
首先,在你的项目XML中添加以下一行,在你编辑项目时预览你的列表:
tools:showIn="@layout/activity_my_recyclerview_item"
然后,在你的RecyclerView XML中添加以下一行来预览你的项目将如何在你的列表中显示:
tools:listitem="@layout/adapter_item"
@oRRs是对的!
我使用Android Studio 1.4 RC2,你现在可以指定任何自定义布局。
我尝试了一个自定义CardView,它工作。
tools:listitem="@android:layout/simple_list_item_checked"
如果你已经有了custom_item布局:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
...
...
**tools:listitem="@layout/name_of_your_custom_item_view"**
...>
</androidx.recyclerview.widget.RecyclerView>
Android工具和LayoutManager
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/* resources轻松可视化布局结构
。
<?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>
模拟结果:
推荐文章
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- Android Studio中的。iml文件是什么?
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点
- 相同的导航抽屉在不同的活动
- 如何从视图中获得托管活动?
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器