【发布时间】:2019-05-08 00:41:25
【问题描述】:
我在 ConstraintLayout 中有三个视图,然后想像这样对齐:
现在视图 B 和 C 形成一个垂直链,而 A 相对于链居中。但是如何将整个组对齐以父级为中心?请注意,视图 C 可能是 GONE。
【问题讨论】:
标签: android android-layout android-constraintlayout
我在 ConstraintLayout 中有三个视图,然后想像这样对齐:
现在视图 B 和 C 形成一个垂直链,而 A 相对于链居中。但是如何将整个组对齐以父级为中心?请注意,视图 C 可能是 GONE。
【问题讨论】:
标签: android android-layout android-constraintlayout
这是一个没有嵌套布局的视觉答案。
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="69dp"
android:layout_height="67dp"
android:background="#fb0000"
android:gravity="center"
android:text="A"
android:textColor="#000000"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textView3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="154dp"
android:layout_height="73dp"
android:background="#2000ff"
android:gravity="center"
android:text="B"
android:textColor="#ffffff"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="@+id/textView3"
app:layout_constraintStart_toStartOf="@+id/textView3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/textView3"
android:layout_width="187dp"
android:layout_height="61dp"
android:background="#f1a500"
android:gravity="center"
android:text="C"
android:textColor="#000000"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</android.support.constraint.ConstraintLayout>
切换到Flutter。布局比 ConstraintLayout 容易得多。
【讨论】:
GONE,那么我认为将 B 和 C 包装在 LinearLayout 中然后使用约束进行其余的布局是最简单的。这也将解决 B 比 C 宽的潜在问题。
使组居中的最简单和最容易理解的方法是通过将视图嵌套在 ConstraintLayout 中使其成为一个组,该 ConstraintLayout 本身嵌套在 ConstraintLayout 中,如下所示:
<android.support.constraint.ConstraintLayout
android:id="@+id/outerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:id="@+id/innerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
[A, B, and C views here...]
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
外部布局可以是另一种类型的ViewGroup,例如使用gravity 的LinearLayout。
其他解决方案是可能的,例如创造性地使用链、障碍或指南,但我认为所概述的解决方案的简单性是最有吸引力的。
【讨论】:
ConstraintLayouts 的全部目的:“ConstraintLayout 允许您创建具有平面视图层次结构的大型复杂布局(无嵌套视图组)。”
gone,那么 Suragch 的解决方案就会中断,OP 说这可能会发生,因此该解决方案存在缺陷,但在其他方面非常出色。如果您有办法使用 100% ConstraintLayout 完成 OP 指定的所有操作,请考虑在此处发布答案。