【问题标题】:Same height and width for cells in grid layout android/kotlin网格布局android / kotlin中单元格的高度和宽度相同
【发布时间】:2019-06-21 20:09:23
【问题描述】:

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

我这样设置布局

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 只是一个形状。

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

我尝试过的其他一些事情:使用卡片,使用线性布局,将内容放在 View 中,与 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>

【问题讨论】:

    标签: android xml kotlin android-constraintlayout android-gridlayout


    【解决方案1】:

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

    将以下用于计算大小的 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
    

    【讨论】:

      猜你喜欢
      • 2011-08-20
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 2022-10-16
      • 2015-02-19
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多