【问题标题】:Android Design Layout ListItem RecyclerView HorizontalAndroid设计布局ListItem RecyclerView Horizo​​ntal
【发布时间】:2016-12-09 23:57:32
【问题描述】:

我正在使用 tools:listitem 属性在设计布局中使用 recyclerview 显示我的视图。问题是,它们总是显示在垂直列表中。有没有办法让设计布局编辑器水平显示它们?

<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"/>

我想要上面的图像,水平显示。在设计视图中。不在应用程序本身中,我知道该怎么做。

【问题讨论】:

  • sonnv1368 链接无效。 Shaishav 不,不是,这个问题不是关于创建一个水平回收视图,而是关于在 Android Studio 的设计视图中显示它。
  • @sonnv1368,就像我上面说的,这不是关于如何创建recyclerview的问题。这是关于在 Android Studio 的 DESIGN VIEW 中显示它。
  • @easycheese:是的,我很抱歉。

标签: android xml android-studio


【解决方案1】:

对于 androidx 版本或 api 28 之后的这几天你应该设置这个属性

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

【讨论】:

    【解决方案2】:

    有效的完整示例:

    <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" />
    

    PS:如果你不使用AndroidX,可以将androidx.recyclerview.widget.LinearLayoutManager替换为android.support.v7.widget.LinearLayoutManager

    【讨论】:

    • 你的答案是最好的。
    【解决方案3】:

    请检查此代码

       <android.support.v7.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
    

    【讨论】:

    • 不会在设计视图中水平显示。
    • 如果你把它和工具结合起来,结果会是这样:listitem="@layout/whatever" 这行得通。
    【解决方案4】:

    是的。

    当你使用 RecyclerView 时,你需要指定一个 LayoutManager 负责布局视图中的每个项目。 LinearLayoutManager 允许您指定方向,就像普通的 LinearLayout 一样。

    要使用 RecyclerView 创建水平列表,您可以执行以下操作:

    LinearLayoutManager layoutManager
    = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    
     RecyclerView myList = (RecyclerView) findViewById(R.id.my_recycler_view);
     myList.setLayoutManager(layoutManager);
    

    【讨论】:

    • 你误读了这个问题。我指的是android studio中的设计视图
    • 你没有阅读我的评论。 Android Studio 布局编辑器中的 DESIGN VIEW。不是代码
    猜你喜欢
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多