【问题标题】:set height of recyclerview images same设置recyclerview图像的高度相同
【发布时间】:2022-01-09 16:24:07
【问题描述】:

我是安卓新手。我在stackoverflow上搜索了很多关于如何设置recyclerview的所有图像的高度相同的内容。 stackoverflow 有很多这样的问题,但是他们的答案对于新手来说太复杂了。

这就是我的代码正在做的事情。 enter image description here

这就是我想要实现的。 enter image description here

我知道网格布局的第一张图片跨度是第二张图片中的 2 和 3,但这不是问题。我希望所有图像的高度相同。

这里是recyclerview的单行xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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="wrap_content">

    <ImageView
        android:id="@+id/sub_imgview"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:fitsSystemWindows="true"
        android:layout_margin="10dp"
        tools:src="@drawable/editorchoice2"
        app:layout_constraintTop_toTopOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: java android android-recyclerview imageview


    【解决方案1】:

    你可以添加app:layout_constraintDimensionRatio来设置ImageView的比例,或者你可以设置ImageView的静态高度。

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/relativeLayoutImagePost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="15dp">
    
        <ImageView
            android:id="@+id/sub_imgview"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_centerHorizontal="true"
            android:adjustViewBounds="true"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintDimensionRatio="H,1:2"
            android:scaleType="centerCrop"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    GridLayoutManager设置为Recyclerview:

    val linearLayoutManage = GridLayoutManager(this, 3)
    recyScroll.layoutManager = linearLayoutManage
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      • 2020-12-12
      • 2010-10-29
      • 2018-12-29
      • 2020-12-25
      • 2017-05-27
      相关资源
      最近更新 更多