Android の ConstraintLayout で特定ウィジェットの中心を軸に高さの異なるウィジェットを並列配置してみた件


方法

ウィジェット A の中央を軸に配置したいウィジェット B にウィジェット A の ID を指定した layout_constraintTop_toTopOflayout_constraintBottom_toBottomOf を設定します。

サンプル

ImageView の中心を軸にして TextViewButton を横並びに設置します。

コード

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp">

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher_round"
            app:layout_constraintEnd_toStartOf="@id/text_view"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/text_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="フェアプレー・ポイントを差し上げます ⚽️"
            android:textAppearance="?android:attr/textAppearanceMedium"
            app:layout_constraintBottom_toBottomOf="@id/image_view"
            app:layout_constraintEnd_toStartOf="@+id/button"
            app:layout_constraintStart_toEndOf="@id/image_view"
            app:layout_constraintTop_toTopOf="@id/image_view" />

        <android.support.v7.widget.AppCompatButton
            android:id="@id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="送信"
            app:layout_constraintBottom_toBottomOf="@id/image_view"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/text_view"
            app:layout_constraintTop_toTopOf="@id/image_view" />
    </android.support.constraint.ConstraintLayout>
</layout>

結果

関連記事