将 2 个视图放在一个 ConstraintLayout 中,该 ConstraintLayout 本身位于另一个 ConstraintLayout 中

Put 2 views in a ConstraintLayout that's itself in another ConstraintLayout

我想了解 Android 的 ConstraintLayout 是如何工作的,为了做到这一点,我想创建一个占视图高度 1/4 的布局left/right/bottom 25dp 的边距,在此布局中,放置两个视图,第一个占其高度的 70%,第二个占剩余的 30%。

总结一下:

到目前为止我试过这个:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.constraint.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginBottom="25dp"
        app:layout_constraintDimensionRatio="w,1:4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" >

        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintDimensionRatio="w,7:10"
            android:background="@color/red" />

        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintDimensionRatio="w,3:10"
            android:background="@color/light_blue" />

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

但是我只有底视图(应该占高度的 30%)占据整个布局 space。我做错了什么?

感谢您的帮助。

你有:

属性 app:layout_constraintDimensionRatio 是 "self dependable" ,也就是它只调整自己的 width-to-height 比例。

你需要什么:

如果您使用的是 ConstrainLayout 1.1.x 您可以使用 属性 app:layout_constraintHeight_percent ,它的值从 0 到 1。

此外,您可能需要调整这些视图顶部和底部的约束以使其相互关联。