【发布时间】:2020-04-20 23:58:22
【问题描述】:
不是它单独的组件,而是
ConstraintLayout本身,或一组元素在一起的尺寸比。
我正在闲置this excellent tutorial 来构建一个带有GridLayoutManager 的RecyclerView 和两个跨越其容器整个宽度的CardView 列,每个CardView(正方形)包含一个ImageView(也是正方形) 和TextView。
所以,我需要 CardView 宽度为“匹配父级”并获得相同的高度(为正方形),ImageView 也是如此。像这样的:
但这就是我目前所拥有的(注意CardView 不是方形的):
这是我的项目布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_home_black_24dp" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
我知道app:layout_constraintDimensionRatio="1:1",但这适用于ConstraintLayout 中的任何元素,例如我的ImageView,而不是ConstraintLayout 本身。
我想我可以颠倒我的布局,将CardView 放在ConstraintLayout 中并将上述属性应用于CardView,但是我需要在CardView 中添加另一个ConstraintLayout 来正确放置ImageView 和TextView,我认为嵌套ConstraintLayout 不是一个好主意。
有什么建议吗?
【问题讨论】:
标签: android kotlin android-recyclerview android-constraintlayout android-cardview