首页 文章

ConstraintLayout与layout_height =“wrap_content”和基于比率的子视图大小调整问题

提问于
浏览
0

我在尝试使用ConstraintLayout 1.1.0-beta4创建布局时遇到了问题 .
我的场景如下:ConstraintLayout包含在ScrollView中,layout_height = "wrap_content"和layout_width = "match_parent" . 在其中,我有一个包含三个子视图的水平链 . 它们应该填满屏幕的整个宽度,并根据它们的宽度确定它们的高度,宽高比为3:4 . 当ConstraintLayout应用layout_height = "match_parent"时,一切正常,但使用wrap_content时,无法计算子视图的大小 . 我期望它以同样的方式工作 .

这是我的XML:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="16dp"
        android:paddingTop="16dp"
        >

        <View
            android:id="@+id/button_1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginEnd="8dp"
            android:background="@android:color/black"
            app:layout_constraintDimensionRatio="3:4"
            app:layout_constraintEnd_toStartOf="@+id/button_2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />

        <View
            android:id="@+id/button_2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:background="@android:color/black"
            app:layout_constraintBottom_toBottomOf="@+id/button_1"
            app:layout_constraintEnd_toStartOf="@+id/button_3"
            app:layout_constraintStart_toEndOf="@+id/button_1"
            app:layout_constraintTop_toTopOf="@+id/button_1"
            />

        <View
            android:id="@+id/button_3"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:background="@android:color/black"
            app:layout_constraintBottom_toBottomOf="@+id/button_1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/button_2"
            app:layout_constraintTop_toTopOf="@+id/button_1"
            />

    </android.support.constraint.ConstraintLayout>

</ScrollView>

为什么ConstraintLayout在这种情况下不能计算子视图的大小?我错过了什么吗?
先感谢您 .

1 回答

  • 1

    看起来constraintLayout确实存在使用维度比率链接视图的问题 . 如果您对解决方案没问题,请创建额外的隐形视图,增加比率并限制视图的顶部/底部:

    <View
            android:id="@+id/viewButtonConstraint"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#FF00FF"
            app:layout_constraintDimensionRatio="W,3:12"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <View
            android:id="@+id/button_1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginEnd="8dp"
            android:background="@android:color/black"
            app:layout_constraintEnd_toStartOf="@+id/button_2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/viewButtonConstraint"
            app:layout_constraintBottom_toBottomOf="@+id/viewButtonConstraint"
            />
    
        ...etc
    

相关问题