layout_height="wrap_content" 和基于比例的子视图大小调整的 ConstraintLayout 问题
Issue with ConstraintLayout with layout_height="wrap_content" and ratio based child view sizing
我在尝试使用 ConstraintLayout 1.1.0-beta4 创建布局时遇到问题。
我的场景如下:一个包含在 ScrollView 中的 ConstraintLayout,其中 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 无法计算子视图的大小?我错过了什么吗?
提前谢谢你。
看起来 constraintLayout 在将视图与维度比率链接起来时确实存在问题。如果您可以解决问题,请创建额外的不可见视图,增加比率并限制 top/bottom 的视图:
<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
我在尝试使用 ConstraintLayout 1.1.0-beta4 创建布局时遇到问题。
我的场景如下:一个包含在 ScrollView 中的 ConstraintLayout,其中 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 无法计算子视图的大小?我错过了什么吗?
提前谢谢你。
看起来 constraintLayout 在将视图与维度比率链接起来时确实存在问题。如果您可以解决问题,请创建额外的不可见视图,增加比率并限制 top/bottom 的视图:
<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