【问题标题】:Set ImageView's width to fill white space (keeping aspect ratio) inside RecyclerView with GridLayoutManager使用 GridLayoutManager 设置 ImageView 的宽度以填充 RecyclerView 内的空白(保持纵横比)
【发布时间】:2016-05-07 02:55:16
【问题描述】:

我有RecyclerViewGridLayoutManger

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>

目前我有这样的结果

我想这样做

【问题讨论】:

    标签: android android-layout android-recyclerview gridlayoutmanager


    【解决方案1】:
    <?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>
    

    这应该可以工作

    【讨论】:

      【解决方案2】:

      用这个替换你的图像视图

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

      【讨论】:

      • 不保持纵横比
      • 如果你想要这样的布局,你得到的图像会更小,在 dp 中手动设置 ImageView 的高度和宽度
      • 是的,但例如 200dp 宽度对 7" 平板电脑有利,对 9" 平板电脑不利,依此类推。那就是问题所在。我想找到根据屏幕上的可用空间自动拉伸图像的解决方案。
      • 所以为不同的屏幕包括不同的 xml,比如 1 代表 7 英寸,1 代表 10 英寸,i 代表手机,您可以将它们放在 res 文件夹中的不同文件夹中,但请确保保留名称xml 相同,android 将自动使用与设备相关的那个。
      【解决方案3】:

      请在您的适配器中将 scaltype 添加到图像视图以适应 fitxy

      【讨论】:

        【解决方案4】:

        看看这个答案:https://stackoverflow.com/a/26575808/4729523

        您只需要保持图像的纵横比(3/4 或 9/16)即可。

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 2013-08-07
            • 2012-06-05
            • 1970-01-01
            • 1970-01-01
            • 2012-03-26
            • 1970-01-01
            • 1970-01-01
            • 2021-07-19
            • 1970-01-01
            相关资源
            最近更新 更多