【Kotlin/Android】TextViewのテキストを左右上下中心に寄せる方法!

この記事からわかること

  • Android Studio/KotlinTextView左右上下中心寄せる方法
  • textAlignment設定値
  • gravityの設定値とlayout_gravityとの違い
  • LinearLayoutを使用していた際の挙動

index

[open]

\ アプリをリリースしました /

みんなの誕生日

友達や家族の誕生日をメモ!通知も届く-みんなの誕生日-

posted withアプリーチ

環境

TextViewのテキストを左右上下中心に寄せる方法

Androidで文字を表示するためのTextViewサイズを固定値にして背景を付与している場合中のテキストの配置を調整したい場合textAlignmentgravity属性の値を変更します。

左右中心に寄せる:textAlignment

テキストを左右の両端や中心に配置するためにはandroid:textAlignment属性を使用します。例えばビューの左右中心にテキストを配置したい場合はcenterを渡します。

【Kotlin/Android】TextViewのテキストを左右上下中心に寄せる方法!
<TextView
  android:id="@+id/text"
  android:layout_width="100dp"
  android:layout_height="50dp"
  android:background="@color/black"
  android:text="Hello"
  android:textAlignment="center"
  android:textColor="@color/white"
  app:layout_constraintEnd_toEndOf="parent"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintTop_toTopOf="parent"
  app:layout_constraintBottom_toTopOf="@+id/linearLayout"/>

指定できる値

inherit: 親要素から継承
gravity: テキストを要素の境界内で垂直方向に中央揃えまたは端揃え?
textStart: テキストを要素の左端に配置
textEnd: テキストを要素の右端に配置
center: テキストを要素の中央に配置
viewStart: テキストを要素のLTRまたはRTLの開始端に配置
viewEnd: テキストを要素のLTRまたはRTLの終了端に配置

android:textAlignment属性では左右の配置しか基本的にはコントロールすることができません。

上下中心に寄せる:gravity

テキストを上下の端や中心に配置するためにはandroid:gravity属性を使用します。それだけでなく左右への配置も操作することができるのでこの属性のみ指定していれば上下左右の配置をコントロールすることができます。例えばビューの上下左右中心にテキストを配置したい場合はcenterを渡します。android:textAlignment属性の指定は必要ありません。

【Kotlin/Android】TextViewのテキストを左右上下中心に寄せる方法!
<TextView
  android:id="@+id/text"
  android:layout_width="100dp"
  android:layout_height="50dp"
  android:background="@color/black"
  android:text="Hello"
  android:gravity="center"
  android:textColor="@color/white"
  app:layout_constraintBottom_toTopOf="@+id/linearLayout"
  app:layout_constraintEnd_toEndOf="parent"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintTop_toTopOf="parent" />

指定できる値

top: テキストを要素の上端に配置
bottom: テキストを要素の下端に配置
left: テキストを要素の左端に配置
right: テキストを要素の右端に配置
center_vertical: テキストを要素の垂直方向に中央揃え
center_horizontal: テキストを要素の水平方向に中央揃え
center: テキストを要素の上下左右中央に配置

指定できる値はandroid:layout_gravity属性と同じようです。

公式リファレンス:android:gravity

gravityとlayout_gravityの違い

似たようなandroid:gravityandroid:layout_gravity属性ですが両者の違いは以下の通りです。

android:gravity

android:gravity自身(要素)の中にあるコンテンツの配置を制御します。そのためTextViewならテキストをLinearLayout自身などに指定した場合は中のビュー要素の配置を指定することができます。

android:layout_gravity

android:layout_gravity親要素内での自身の位置を制御します。そのためLinearLayoutの子要素などに指定した場合は親要素内での自身を配置を指定することができます。

【Kotlin/Android】TextViewのテキストを左右上下中心に寄せる方法!
<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/button1"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:background="@color/black"
        android:text="Hello"
        android:textAlignment="center"
        android:textColor="@color/white" />

    <TextView
        android:id="@+id/button2"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:background="@color/black"
        android:text="World"
        android:textColor="@color/white" />

    <TextView
        android:id="@+id/button3"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:background="@color/black"
        android:text="!!"
        android:textColor="@color/white" />
</LinearLayout>

まだまだ勉強中ですので間違っている点や至らぬ点がありましたら教えていただけると助かります。

ご覧いただきありがとうございました。

searchbox

スポンサー

ProFile

ame

趣味:読書,プログラミング学習,サイト制作,ブログ

IT嫌いを克服するためにITパスを取得しようと勉強してからサイト制作が趣味に変わりました笑
今はCMSを使わずこのサイトを完全自作でサイト運営中〜

New Article

index