【发布时间】:2016-06-11 10:08:36
【问题描述】:
我发现了如何使用预览列表项
tools:listitem="@layout/my_item_layout"
然而,Android Studio 将 recyclerview 预览为垂直列表。有没有办法告诉 Android Studio 使用LinearLayoutManager 以水平方式显示布局预览?
【问题讨论】:
标签: android android-studio android-recyclerview
我发现了如何使用预览列表项
tools:listitem="@layout/my_item_layout"
然而,Android Studio 将 recyclerview 预览为垂直列表。有没有办法告诉 Android Studio 使用LinearLayoutManager 以水平方式显示布局预览?
【问题讨论】:
标签: android android-studio android-recyclerview
添加一个 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"
/>
【讨论】:
tools:orientation 和tools:layoutManager)添加它们,然后它只会影响 IDE 预览,您可以继续在代码中设置这些值。
app:layoutManager="android.support.v7.widget.LinearLayoutManager" 替换为 app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" :)
如果您使用的是 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"
/>
【讨论】:
tools:listitem 不适用于androidx.recyclerview:recyclerview:1.1.0-alpha01。如果我不使用tools:listitem,我只能在设计模式下看到水平列表
android:layout_width="match_parent"。通过将其更改为android:layout_width="wrap_content",我无法使用androidx.recyclerview:recyclerview:1.1.0-alpha01 在Android Studio 的设计选项卡中水平滚动。
只需在您的布局中使用 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"
【讨论】:
否则你可以在java文件中使用:
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
【讨论】: