具有 dimensionRatio 子视图的约束布局 "wrap_content"
Constraint Layout "wrap_content" with dimensionRatio child view
在 ConstraintLayout 版本 < beta5 中,我有如下示例的布局:
<android.support.constraint.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">
<ImageView
android:id="@+id/square_image"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintDimensionRatio="H, 1:1"/>
</android.support.constraint.ConstraintLayout>
但是,从 beta5 开始的版本删除了子视图的 MATCH_PARENT 限制。
文档给出了使用 app:layout_constraintDimensionRatio:
的示例
You can also use ratio if both dimensions are set to MATCH_CONSTRAINT
(0dp). In this case the system sets the largest dimensions the
satisfies all constraints and maintains the aspect ratio specified. To
constrain one specific side based on the dimensions of another. You
can pre append W," or H, to constrain the width or height
respectively. For example, If one dimension is constrained by two
targets (e.g. width is 0dp and centered on parent) you can indicate
which side should be constrained, by adding the letter W (for
constraining the width) or H (for constraining the height) in front of
the ratio, separated by a comma:
<Button android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,16:9"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
此示例在父布局具有固定高度或 match_parent
时有效,但在父布局设置为 wrap_content
时无效。
使用我上面的代码示例,如果我将 ImageView 宽度设置为 0dp,父视图就会折叠起来,就好像它没有内容一样。
这是一个非常有用的功能,我觉得我在这个新版本中缺少了一些东西。任何帮助表示赞赏。
ConstraintLayout v1.0.2
修复了这个问题。
在 ConstraintLayout 版本 < beta5 中,我有如下示例的布局:
<android.support.constraint.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">
<ImageView
android:id="@+id/square_image"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintDimensionRatio="H, 1:1"/>
</android.support.constraint.ConstraintLayout>
但是,从 beta5 开始的版本删除了子视图的 MATCH_PARENT 限制。
文档给出了使用 app:layout_constraintDimensionRatio:
的示例You can also use ratio if both dimensions are set to MATCH_CONSTRAINT (0dp). In this case the system sets the largest dimensions the satisfies all constraints and maintains the aspect ratio specified. To constrain one specific side based on the dimensions of another. You can pre append W," or H, to constrain the width or height respectively. For example, If one dimension is constrained by two targets (e.g. width is 0dp and centered on parent) you can indicate which side should be constrained, by adding the letter W (for constraining the width) or H (for constraining the height) in front of the ratio, separated by a comma:
<Button android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,16:9"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
此示例在父布局具有固定高度或 match_parent
时有效,但在父布局设置为 wrap_content
时无效。
使用我上面的代码示例,如果我将 ImageView 宽度设置为 0dp,父视图就会折叠起来,就好像它没有内容一样。
这是一个非常有用的功能,我觉得我在这个新版本中缺少了一些东西。任何帮助表示赞赏。
ConstraintLayout v1.0.2
修复了这个问题。