Androidで文字の配置の変更
ボタンやTextViewなどで表示される文字の配置場所を変更するには
android:gravity=””
で設定する
使い方としては
文字を左揃え、中央揃え、右揃えとしたり
上下、中央、左右の好きな場所へ配置するなどなど
なお、配置場所は |
これで区切ることで、組合せを作り
右上とかにすることができる
右上にするなら、上と右の組合せなので
“right|top”
とする
とりあえず、TextViewでそれぞれの例を
レイアウトファイルで書いてみる
ちなみに、
layout_width=”wrap_content”
とか
layout_height=”wrap_content”
にすると、幅をあわせてしまうので
たいていは表示する大きさを指定することになる
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | < TextView android:id = "@+id/text" android:layout_width = "200dip" android:layout_height = "50dip" android:gravity = "left|top" android:text = "左上" /> < TextView android:id = "@+id/text2" android:layout_width = "200dip" android:layout_height = "50dip" android:gravity = "right|top" android:text = "右上" /> < TextView android:id = "@+id/text3" android:layout_width = "200dip" android:layout_height = "50dip" android:gravity = "right|bottom" android:text = "右下" /> < TextView android:id = "@+id/text4" android:layout_width = "200dip" android:layout_height = "50dip" android:gravity = "left|bottom" android:text = "左下" /> |
というかんじ
TextView以外にはリスト表示するときとか
画像と合わせて表示するときなどに使える