首页 > 解决方案 > 网格布局android / kotlin中单元格的高度和宽度相同

问题描述

如何获得具有网格布局的回收器视图,其中每个单元格具有相同的单元格宽度和单元格高度,具体取决于屏幕宽度。

我这样设置我的布局

linearLayoutManager = LinearLayoutManager(this)
categoriesRecyclerView.layoutManager = GridLayoutManager(this,4)

而且我总是连续获得 4 个单元格。

结果看起来像这样。

在此处输入图像描述

我明白为什么我会得到这个结果。我将单元格内的内容始终设置为wrap_content. 因此,当图像较大或文本较长时,高度会发生变化。

我的布局是这样定义的:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintDimensionRatio="1:1"
android:layout_margin="8dp"
android:background="@drawable/rounded_image_view">

rounded_image_view只是一个形状。

我认为我可以借助比率来调节约束布局。

我尝试过的其他一些事情:使用卡片,使用线性布局,将内容放在 aView中,使用过,constraintHeight_percent并且我在类似的线程中阅读了很多。但我没有得到解决方案。

完整的 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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintDimensionRatio="1:1"
android:layout_margin="8dp"
app:layout_constraintHeight_percent="0.5"
android:background="@drawable/rounded_image_view">

<ImageView
    android:id="@+id/categoryImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toTopOf="@id/itemTitle"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/anatomie" />

<TextView
    android:id="@+id/itemTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Anatomie"
    android:textColor="@color/colorAccent"
    android:textSize="12sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/categoryImage" />

<ImageView
    android:id="@+id/isPremiumImage"
    android:layout_width="20dp"
    android:layout_height="20dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/locked" />
</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidxmlkotlinandroid-constraintlayoutandroid-gridlayout

解决方案


您可以动态计算设备屏幕的宽度并相应地设置项目视图的高度。

将以下用于计算大小的 util 方法添加到您的适配器类。

     /**
     * calculates the size of the item based on the screen size
     */
    fun calculateSizeOfView(context: Context): Int {

        val displayMetrics = context.resources.displayMetrics
        val dpWidth = displayMetrics.widthPixels
        return (dpWidth / COLUMN_COUNT) // COLUMN_COUNT would be 4 in your case
    }

现在在适配器类的 onCreateViewHolder 方法中添加以下代码行。

1) 从 util 方法中获取每个项目的大小

2)创建布局参数 - 高度和宽度都如上计算值

3) 将参数设置为 OnCreateViewHolder 内的膨胀视图

val view = *your_inflated_view* // the return view of your .inflate method
val size = calculateSizeOfView(*your_context*)

val margin = 8 * 4 // any vertical spacing margin = your_margin * column_count 
val layoutParams = GridLayout.LayoutParams(ViewGroup.LayoutParams(size - margin, size)) // width and height

layoutParams.bottomMargin = 8 // horizontal spacing if needed

view.layoutParams = layoutParams

return *your_view_holder_with_view* // usual return

推荐阅读