ConstraintLayout - 垂直或水平相邻的居中视图

Dar*_*ish 15 android centering android-layout android-constraintlayout

如何将3个按钮中心对齐并使用ConstraintLayout

为了清楚起见,我想将这个简单的布局结构转换为平面UI ConstraintLayout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

我已经获得了如下最近的解决方案

<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">


<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/button2"
    />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="parent"
    tools:layout_conversion_absoluteHeight="48dp"
    tools:layout_conversion_absoluteWidth="88dp"
    tools:layout_conversion_absoluteX="148dp"
    tools:layout_conversion_absoluteY="259dp"
    app:layout_constraintBottom_toTopOf="@+id/button3"
    app:layout_constraintTop_toBottomOf="@+id/button"
    app:layout_constraintRight_toRightOf="parent"/>

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    app:layout_constraintLeft_toLeftOf="parent"
    tools:layout_conversion_absoluteHeight="48dp"
    tools:layout_conversion_absoluteWidth="88dp"
    tools:layout_conversion_absoluteX="148dp"
    tools:layout_conversion_absoluteY="307dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button2"
    app:layout_constraintRight_toRightOf="parent"/>

 </android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

但很明显,您可以看到获得的输出与所需的输出不匹配.我不希望3个按钮之间有任何边距或空格,我想要的是将这3个按钮垂直居中对齐,就像它们处于LinearLayout垂直方向一样.

Mac*_*ęga 33

适当的解决方案

在这3个视图之间创建链条是很好的.有了一个链,您可以指定链"样式",它将影响视图沿链轴的分布方式.链式样式可以通过视图正下方的"链"按钮控制:

在此输入图像描述

点击几下可在所有3种模式之间切换:

传播(默认的)
在此输入图像描述

spread_inside
在此输入图像描述

打包
在此输入图像描述

如你所见 - packed是你想要的那个.
设置链样式将导致向链中的第一个子项添加以下属性,因此您也可以从XML中执行此操作:

app:layout_constraintVertical_chainStyle="packed"

天真的解决方案

在另一个答案中提出的解决方案看起来可能有效,但实际上它不适合您的问题.考虑具有不同高度的视图的情况,假设底部的视图更大.该解决方案将中间视图锁定在中心并定位上方和下方的其他视图.它不会产生"居中的组"(只有中间视图会居中).您可以在下图中看到比较:

在此输入图像描述


Dar*_*ish 9

通过Android Studio的"布局编辑器"

  1. 将三个按钮拖放到Android Studio的"布局编辑器"中

  2. 通过拖动鼠标一起选择这些按钮

  3. 使用"布局编辑器"中的"包"按钮垂直打包它们

  4. 使用"水平对齐中心"按钮将它们水平对齐

  5. 使用"垂直对齐中心"按钮将它们垂直对齐

通过xml


<?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:layout_editor_absoluteY="25dp"
tools:layout_editor_absoluteX="0dp">


<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintVertical_chainStyle="packed"
    app:layout_constraintBottom_toTopOf="@+id/button2"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"/>

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:layout_conversion_absoluteHeight="48dp"
    tools:layout_conversion_absoluteWidth="88dp"
    tools:layout_conversion_absoluteX="148dp"
    tools:layout_conversion_absoluteY="259dp"
    app:layout_constraintVertical_chainStyle="packed"
    app:layout_constraintBottom_toTopOf="@+id/button3"
    app:layout_constraintTop_toBottomOf="@+id/button1"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"/>

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:layout_conversion_absoluteHeight="48dp"
    tools:layout_conversion_absoluteWidth="88dp"
    tools:layout_conversion_absoluteX="148dp"
    tools:layout_conversion_absoluteY="307dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button2"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

  • 在 `ConstraintLayout` 中,你应该只使用 `chainStyle` 属性,另一种解决方案实际上更像是一种实现“以组为中心”的黑客,似乎只是从问题中解决问题。如果有正确的解决方案与错误的解决方案,为什么有人会想要使用不正确的解决方案......?链是专门为解决“分组”问题而设计的。然而,您仍然将不正确的解决方案宣传为可接受的答案以及答案的一部分。它只会愚弄将来尝试解决此问题的其他开发人员,这确实不好。 (2认同)