使用GridLayoutManager在RecyclerView中,将ImageView的宽度设置为填充白色空间(保持纵横比)。

4

我有一个带有GridLayoutMangerRecyclerView

RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.movie_posters_recycler_view);
recyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));

每个RecyclerView的项由以下布局表示。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/movie_item_poster_image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true" />

</FrameLayout>

目前我有这样的结果

layout1

我想做到这一点

layout2

5个回答

4
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/movie_thumbnail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"/>
</FrameLayout>

这应该可以正常工作


0
请在您的适配器中为图像视图添加scaltype以适应fitxy。

0

0

ImageView属性中使用android:scaleType="fitXY"应该会将图像拉伸到ImageView内。


0

请使用此视图替换您的图片视图

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/movie_item_poster_image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true" />

它不保持纵横比。 - Yuriy Seredyuk
你获取的图片太小了,如果你想要一个像那样的布局,请手动设置你的ImageView的高度和宽度(以dp为单位)。 - Atiq
是的,但例如,200dp的宽度适合7英寸平板电脑,但对于9英寸平板电脑来说就不好了,这就是问题所在。我想找到一种解决方案,根据屏幕上的可用空间自动拉伸图像。 - Yuriy Seredyuk
为不同的屏幕包含不同的XML文件,例如7英寸和10英寸各一个,以及手机一个。您可以将它们放在res文件夹中的不同文件夹中,但请确保XML文件名相同,Android会自动根据设备使用相应的文件。 - Atiq

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