安卓设计布局ListItem RecyclerView Horizontal

29

我正在使用tools:listitem属性在RecyclerView中显示视图,但是问题是它们总是以垂直列表的形式显示。有没有办法让Design Layout Editor将它们水平显示?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:listitem="@android:layout/simple_list_item_checked"/>

enter image description here

我想把上面这张图片横向显示在设计视图中,而不是在应用程序本身中显示,我知道如何在应用程序中完成。


这个有帮助吗:https://dev59.com/Jl4b5IYBdhLWcg3w-1-c? - Shaishav
Shaishav,不是的,这个问题不是关于创建水平RecyclerView,而是关于在Android Studio的设计视图中显示它。 - easycheese
3个回答

29

完整的可运行示例:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:orientation="horizontal"
    tools:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:listitem="@layout/view_item" />

提示:如果您不使用 AndroidX ,则可以将androidx.recyclerview.widget.LinearLayoutManager替换为android.support.v7.widget.LinearLayoutManager


1

对于 androidx 版本或 API 28 之后的版本,您应该设置此属性

    tools:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

0

为RecyclerView应用这些属性:

 tools:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
 android:orientation="horizontal"

例子:

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvPokemonTeamPokemon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintEnd_toEndOf="parent"
        tools:listitem="@layout/item_pokemon_image"
        tools:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:orientation="horizontal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:itemCount="6" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接