在 Android 中对齐两个视图的垂直中心

Align vertical center of two view in Android

如何在 LinearLayout 或 RelativeLayout 中对齐两个视图的垂直中心(iOS 等同于 view1.centerY = view2.centerY)?

我正在尝试实现以下布局。如何使第一行的所有三个视图在 xml 中垂直对齐?第一行的中间视图是固定的。我想要左右视图的垂直中心线等于中间视图的垂直中心线。

使用 Android's relatively new ConstraintLayout you should be able to treat the top and bottom edges of the blue layout as top and bottom constraints respectively for the red and violet ones. Aligning both of them to both of these constraints at once, while defining their heights in absolute, should render the layout you are looking for. You can refer to the official examples repository 检查此布局类型的用例。

编辑: 花点时间为您提供了一个简化的示例。仅包括与案例相关的tags/parameters。

<android.support.constraint.ConstraintLayout 
...
>
    <Layout
    android:id="@+id/blue"
    ...
    />

    <Layout
    android:id="@+id/red"
    android:layout_height="20dp"
    app:layout_constraintTop_toTopOf="@+id/blue"
    app:layout_constraintBottom_toBottomOf="@+id/blue"
    ...
    />

    <Layout
    android:id="@+id/violet"
    android:layout_height="20dp"
    app:layout_constraintTop_toTopOf="@+id/blue"
    app:layout_constraintBottom_toBottomOf="@+id/blue"
    ...
    />


</android.support.constraint.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:id="@+id/view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ConstraintLayout">

    <View
        android:id="@+id/left"
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:background="#FF2600"
        app:layout_constraintBottom_toBottomOf="@+id/center"
        app:layout_constraintEnd_toStartOf="@+id/center"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/center" />

    <View
        android:id="@+id/center"
        android:layout_width="100dp"
        android:layout_height="60dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="#0633FF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/right"
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:layout_marginEnd="8dp"
        android:background="#FF40FF"
        app:layout_constraintBottom_toBottomOf="@+id/center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/center"
        app:layout_constraintTop_toTopOf="@+id/center" />
</android.support.constraint.ConstraintLayout>

另一种选择是使用框架布局在 RelativeLayout 中垂直对齐视图

        <TextView
            android:id="@+id/labelBrightnessSetting"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Display Brightness"
            />

        <ImageView
            android:id="@+id/btnReduceBrightness"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_below="@+id/labelBrightnessSetting"
            android:layout_marginTop="25dp"
            android:src="@drawable/vector_minus" />

        <!-- This will center align all contents-->
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_below="@+id/labelBrightnessSetting"
            android:layout_marginTop="25dp"
            android:layout_toLeftOf="@+id/btnIncreaseBrightness"
            android:layout_toRightOf="@+id/btnReduceBrightness">

            <SeekBar
                android:id="@+id/brightnessSeekbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="@dimen/margin_5px"
                android:layout_marginRight="@dimen/margin_5px" />
        </FrameLayout>

        <ImageView
            android:id="@+id/btnIncreaseBrightness"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_below="@+id/labelBrightnessSetting"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop="25dp"
            android:src="@drawable/vector_plus" />

    </RelativeLayout>