首先,我不认为你试图给第一个视图的尺寸增加任何限制。如果你想它有一个3:4的固定长宽比,你应该设置
app:layout_constraintDimensionRatio="H,3:4"
到您想要的视图并删除
app:layout_constraintDimensionRatio="0.75".
因此,您不再需要占位符视图。所以你的布局应该是这样的:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/view1"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,3:4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/view2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view1" />
但是如果你想让你的视图1保持在75%的屏幕比例,那么你应该用一个水平的指导来代替你的占位符视图
app:layout_constraintGuide_percent="0.75"
. 所以布局如下:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/view1"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.75" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/view2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view1" />
同时请记住使用
match_parent
在一个
ConstraintLayout
不推荐。相反,请使用0dp高度/宽度维度,并根据情况为边距设置适当的约束(顶部、底部/开始、结束)