ConstraintLayoutで垂直方向で中央に集める
環境
Android Studio 4.1.2
やりたいこと
ConstraintLayoutでViewを上下真ん中に集めたい。
これを
こうしたい
やったこと
いじる前のレイアウトはこちら。
<?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でやる場合
- 全部選ぶ
- 右クリックメニューから [Chains] -> [Create Vertical Chain]
するとこんな感じになる
- 右クリックメニューから[Chains] -> [Vertical Chain Style] -> [packed]
すると真ん中に寄る
手動でやる場合
- チェーンな関係にする
layout_constraintVertical_chainStyle
にpacked
を指定する
チェーンとは、『双方向の位置制約を設定して、相互にリンクさせたビューのグループ』のこと。(参照: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>
参考
Android Layout Cookbook アプリの価値を高める開発テクニック
- 作者:あんざい ゆき
- 発売日: 2011/03/11
- メディア: 単行本(ソフトカバー)