我发现可以通过使用以下方法预览列表项:
tools:listitem="@layout/my_item_layout"
Android Studio预览RecyclerView为垂直列表。是否有一种方法可以告诉Android Studio使用LinearLayoutManager
以水平方式显示布局预览?
我发现可以通过使用以下方法预览列表项:
tools:listitem="@layout/my_item_layout"
Android Studio预览RecyclerView为垂直列表。是否有一种方法可以告诉Android Studio使用LinearLayoutManager
以水平方式显示布局预览?
添加一个LayoutManager并设置为水平方向。
这里是一个例子:
<android.support.v7.widget.RecyclerView
android:id="@+id/homesRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:layout_centerVertical="true"
/>
如果您正在使用AndroidX库:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/homesRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:layout_centerVertical="true"
/>
androidx.recyclerview:recyclerview:1.1.0-alpha01
时,tools:listitem
对我没有起作用。如果我不使用tools:listitem
,我只能在设计模式下看到水平列表。 - Michael Osofskyandroid:layout_width="match_parent"
。将其更改为 android:layout_width="wrap_content"
后,我可以在 Android Studio 的设计选项卡中使用 androidx.recyclerview:recyclerview:1.1.0-alpha01
进行水平滚动。 - Michael Osofsky只需在布局中使用app:layoutManager
和android:orientation
属性,并且还要使用tools
名称空间添加它们。
类似于以下内容:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/ly_item"
tools:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:orientation="horizontal"
../>
被接受的答案非常准确。对于androidx,只需在您的recyclerView中使用以下内容:
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="horizontal"
AndroidX
<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="wrap_content"
tools:layoutManager="android.support.v7.widget.LinearLayoutManager"
tools:listitem="@layout/item"
tools:orientation="horizontal"/>
RecyclerView
的预览,请按照以下步骤进行:
xmlns:tools="http://schemas.android.com/tools"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="horizontal"
例子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerQueueItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="10"
tools:listitem="@layout/item_queue_token_number"
/>
</LinearLayout>
否则你可以在Java文件中使用:
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
tools:orientation
和tools:layoutManager
)来添加它们,这样只会影响IDE预览,你仍然可以在代码中设置这些值。 - gMaleapp:layoutManager="android.support.v7.widget.LinearLayoutManager"
替换为app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
:) - stkent