6
2

More than 5 years have passed since last update.

Android の ConstraintLayout でウィジェットの中心にウィジェットを配置してみた件

Last updated at Posted at 2018-02-25

方法

中央に配置されるウィジェットに4方向分の layout_constraint*_to*Of="中央にウィジェットを配置するウィジェットの ID" を指定することでセンタリングを行います。

サンプル

ImageView のセンターに Button を表示してみます。

コード

sample.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/bg_pattern2_aozora" />

    <android.support.v7.widget.AppCompatButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中央"
        app:layout_constraintBottom_toBottomOf="@id/imageView"
        app:layout_constraintEnd_toEndOf="@id/imageView"
        app:layout_constraintStart_toStartOf="@id/imageView"
        app:layout_constraintTop_toTopOf="@id/imageView" />
</android.support.constraint.ConstraintLayout>

結果

スクリーンショット 2018-02-25 16.53.33.png

関連記事

6
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2