ConstraintLayoutで垂直方向で中央に集める

環境

Android Studio 4.1.2

やりたいこと

ConstraintLayoutでViewを上下真ん中に集めたい。
これを
f:id:mst335:20210223105447p:plain

こうしたい
f:id:mst335:20210223105558p:plain

やったこと

いじる前のレイアウトはこちら。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2" />
</androidx.constraintlayout.widget.ConstraintLayout>

GUIでやる場合

  1. 全部選ぶ
    f:id:mst335:20210223110222p:plain
  2. 右クリックメニューから [Chains] -> [Create Vertical Chain]
    f:id:mst335:20210223110457p:plain
    するとこんな感じになる
    f:id:mst335:20210223111433p:plain
  3. 右クリックメニューから[Chains] -> [Vertical Chain Style] -> [packed]
    f:id:mst335:20210223111118p:plain
    すると真ん中に寄る
    f:id:mst335:20210223105558p:plain

手動でやる場合

  1. チェーンな関係にする
  2. layout_constraintVertical_chainStylepacked を指定する

チェーンとは、『双方向の位置制約を設定して、相互にリンクさせたビューのグループ』のこと。(参照:ConstraintLayout でレスポンシブ UI を作成する)
button1の layout_constraintBottom_toTopOf にbutton2を、button2の layout_constraintTop_toBottomOf にbutton1を指定した関係がチェーンとなる。
今回の場合Viewが3つあるので、button <-> button1と、button1 <-> button2 をそれぞれチェーンにする必要がある。

チェーンに対してスタイルを指定することができる。ここに packed を指定することで期待する中央寄せの状態になる。

app:layout_constraintVertical_chainStyle="packed"

結果

垂直方向で中央に集まった状態のレイアウトはこちら。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/button2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/button3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2" />
</androidx.constraintlayout.widget.ConstraintLayout>

参考

developer.android.com

   

   

   

   

   

   

Android Layout Cookbook アプリの価値を高める開発テクニック

Android Layout Cookbook アプリの価値を高める開発テクニック