当我使用ScrollView和HorizontalScrollView进行水平和垂直滚动并切换到横向时,RecyclerView不会像纵向那样充满屏幕。
我在一行布局中使用元素的相对宽度,并且所有内容在纵向方向上看起来都不错。
片段布局
string
单行布局
table table apple chair cupboard
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:scrollbars="vertical"
android:fillViewport="true">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/table_border">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/tableView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="horizontal"
android:background="@drawable/table_border"
tools:listitem="@layout/table_inventory_row"
/>
</HorizontalScrollView>
</ScrollView>
肖像https://imgur.com/5VaEJ6S https://imgur.com/97WGJ1n
如您在屏幕快照中所见,我在水平滚动条上放置了蓝色边框,它占据了整个屏幕,但RecyclerView与宽度不匹配。
如何使RecyclerView占据父级的全部宽度?
答案 0 :(得分:1)
您的问题是如何创建每一行。您正在得到您所要求的。行的大小都相同。在纵向模式下,您的回收站视图大于屏幕宽度。但是,在横向模式下,您的行的大小完全相同,但是不会覆盖整个屏幕宽度。
您应该做的是在文本视图的宽度上使用wrap_content
,而不是设置粗细。设置权重时,您将创建一个静态尺寸。使用wrap_content
,您将包装所有内容。
另外,在片段中,请确保您在旋转时通过以下方式创建视图的新实例:
Fragment.setRetainInstance(false);
答案 1 :(得分:0)
首先将您的 RecyclerView 布局准备为
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="@color/colorWhite">
....
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layoutAnimation="@anim/layout_animation" />
</HorizontalScrollView>
.....
</RelativeLayout>
然后,添加
android:configChanges="orientation|screenSize"
到您的 Manifests.xml
这一切都可以从活动/片段中设置您的回收站视图。
有关更多详细信息,这是给您的Article。