android - android.support.v7.widget.RecyclerView 作为根视图
问题描述
android.support.v7.widget.RecyclerView 可以是布局文件中的根视图,还是我必须将其添加为“RelativeLayout”元素的子级?
布局 A
<android.support.v7.widget.RecyclerView
android:id="@+id/itemsRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
app:spanCount="2"/>
布局 B
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activity.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/itemsRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
app:spanCount="2"/>
</RelativeLayout>
解决方案
正如官方文档所说:
根元素可以是 ViewGroup、View 或元素,但必须只有一个根元素,并且它必须包含 xmlns:android 属性和 android 命名空间,如图所示。
所以你可以使用 RecyclerView 作为根视图:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</android.support.v7.widget.RecyclerView>
推荐阅读
- python - 使用 loc 方法获取 DataFrame 的视图
- c# - 单击时如何激活布娃娃
- export-to-csv - 将数据框导出到文本文件
- java - Drools 规则 - 空检查和累积条件
- docker - Amazon S3 - 将存储连接到 docker 映像,如网盘
- reactjs - React 弹窗根据 IP 地址只显示一次
- c# - 是否可以从包管理器控制台运行角度命令?
- elasticsearch - 有什么方法可以将 Kibana 仪表板原样导出到 Grafana 吗?
- arm - NEON 内在函数中的“最大”与“最大数量”
- c - merge_sort 的代码没有正确排序