Androidの背景設定
背景設定は
android:background=””
で設定する
設定できるのは
通常の画像
shapeリソース
9patch
まずはShapeの場合
これには、まず背景用のShapeリソースを作成する
ソースは
/res/drawable/shape.xml
1 2 3 4 5 6 7 8 9 | < shape < gradient android:angle = "270" android:endColor = "#FF2A68" android:startColor = "#FF53A" /> < corners android:radius = "10dp" /> </ shape > |
次に、作成したレイアウトファイルを
背景に設定するので
android:background=””
で指定する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | < TextView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_margin = "@dimen/padding_xlarge" android:background = "@drawable/shape" android:padding = "@dimen/padding_large" android:text = "shapeを使った背景" /> < TextView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_margin = "@dimen/padding_xlarge" android:background = "@drawable/normal" android:text = "普通の背景画像" /> < TextView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_margin = "@dimen/padding_xlarge" android:background = "@drawable/9patch" android:text = "9patchでの画像" /> |
今回は一緒に
普通の場合と9patchの場合も追加
shapeリソースのメリットは
XMLなのでファイルが軽いこと
そして、Viewのサイズに依存しないので画質が一定になること
これは、グラデーション背景とか
角丸ボタンの作成に使われる
そして、9patchのメリットは
伸縮するのが前提なので、小さい画像ファイルでできるため
こちらもファイルサイズを小さくできる
またshapeみたいにViewのサイズに依存しないので画質が一定になる
さらに、コンテンツの表示領域の指定も可能
これは、画像こ一部だけをのばしたり
EditTextで特定の場所だけ入力欄にするときに使う
9patchリソースは
Android SDK の中の toolsディレクトリの中にあるツールの
draw9patch
を使うことで作成できる