【问题标题】:Is it possible to set the dimension ratio of a ConstraintLayout itself?是否可以设置 ConstraintLayout 本身的尺寸比?
【发布时间】:2020-04-20 23:58:22
【问题描述】:

不是它单独的组件,而是ConstraintLayout本身,或一组元素在一起的尺寸比。

我正在闲置this excellent tutorial 来构建一个带有GridLayoutManagerRecyclerView 和两个跨越其容器整个宽度的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 来正确放置ImageViewTextView,我认为嵌套ConstraintLayout 不是一个好主意。

有什么建议吗?

【问题讨论】:

    标签: android kotlin android-recyclerview android-constraintlayout android-cardview


    【解决方案1】:

    试试下面的项目布局。我限制了ImageView 相对于TextView。它应该工作。或者您可能需要为ImageView 添加一些边距。试一试。

    <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">
    
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:cardPreventCornerOverlap="true"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintTop_toTopOf="parent"
            app:cardUseCompatPadding="true">
    
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:padding="8dp"
                android:layout_height="match_parent">
    
                <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"
                    android:background="@color/colorPrimary"
                    android:layout_marginBottom="8dp"
                    app:layout_constraintBottom_toTopOf="@+id/textView" />
    
                <TextView
                    android:id="@+id/textView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textSize="23sp"
                    android:text="Hello world"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent" />
    
            </androidx.constraintlayout.widget.ConstraintLayout>
    
        </androidx.cardview.widget.CardView>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • 是的,它有效,但这正是我在问题末尾的想法:“我想我可以反转我的布局,将CardView 放入ConstraintLayout 并应用上述属性 (app:layout_constraintDimensionRatio="1:1") 到 CardView (...)"。所以,这里的问题是这样做是否正确(将一个 ConstraintLayout 嵌套在另一个中),或者我相信,我们将否定 ConstraintLayout 保持平面布局的目的。跨度>
    • 是的,但在这种情况下需要嵌套。因为根布局是动态的。看看This thread。你会发现类似的答案..
    猜你喜欢
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2013-03-01
    相关资源
    最近更新 更多