【发布时间】:2018-08-18 17:45:48
【问题描述】:
ConstraintLayout 很强大,但有时也很棘手。
我想用ConstraintLayout 实现一个布局,这可以用LinearLayout 轻松实现。
-
蓝色区域是父约束布局。红色部分是LinearLayout。我想通过保持以下条件将此 LinearLayout 转换为 ConstraintLayout
- 所有子项(按钮)的宽度应与最宽的子项相匹配。
- 最宽的孩子不固定。它将在运行时更改。
-
红框应保持
wrap_content。
我尝试过使用约束布局的屏障、指南和其他属性,但没有成功。
这是带有LinearLayout的代码:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#476dce"
android:padding="16dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="#f5aaaa">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
tools:text="Small" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
tools:text="Bigggggeer" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
tools:text="Even Bigggggggggeer" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
【问题讨论】:
-
为什么要替换线性布局?
-
是的。我想放置 ConstraintLayout 而不是 LinearLayout。
-
如果您知道哪个子宽度最大,您可以这样做。将该子宽度设置为
wrap_content并设置剩余的子宽度以匹配约束。
标签: android android-layout android-constraintlayout