[英]android.support.v7.widget.RecyclerView as the root view
Can the android.support.v7.widget.RecyclerView be the root view in a layout file or I must add it as a child of "RelativeLayout" element? android.support.v7.widget.RecyclerView可以作为布局文件中的根视图吗,还是必须将其添加为“ RelativeLayout”元素的子级?
Layout A 布局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"/>
Layout B 版面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>
As official doc says: 如官方文档所述:
The root element can be either a ViewGroup, a View, or a element, but there must be only one root element and it must contain the xmlns:android attribute with the android namespace as shown. 根元素可以是ViewGroup,View或一个元素,但是必须只有一个根元素,并且必须包含xmlns:android属性以及android命名空间,如图所示。
so you could use RecyclerView as the root view: 因此您可以将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>
Yes you can take recyclview as root element of activity.But it's a good practice to take any view group as a root and recyclview as child view. 是的,您可以将recyclview作为活动的根元素。但是,将任何视图组作为根并将recyclview作为子视图是一个好习惯。
Here is code: 这是代码:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.