android - 网格布局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>
解决方案
您可以动态计算设备屏幕的宽度并相应地设置项目视图的高度。
将以下用于计算大小的 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
推荐阅读
- c# - 在 IIS 上部署 ASP.NET MVC 应用程序后出现空白页
- java - 删除项目时JavaFX TableView自定义单元格颜色故障
- model-view-controller - 我无法在 grails 中显示来自不同域的数据
- java - 如何上传视频到解析服务器?
- javascript - 如何使用 accounts-ui 在 Meteor 中使用特定用户
- javascript - 如何使用 ajax 和 PUT 动词更新数据库?
- c# - 在生产 CMS 方法中应用迁移
- c++ - 在 2D OpenGL 中处理与顺序无关的透明度的有效方法是什么?
- haskell - 箭头定律:首先仅取决于对的第一个分量。为什么我们需要这个?
- java - 如何链接可观察对象?