Androidアプリ入門 No.05 スタイル
今風に書き直した記事がありますので、合わせて参照ください。
blog.webarata3.link
ウィジェット共通
スタイル
スタイルとして属性をまとめて定義することができる。よく使用するパターンなどはスタイルとして定義するとよい。ほぼドキュメントと同じ例だが以下に例を示す。
まず、スタイルをstrings.xmlに定義する。
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">ActivityTest</string> <style name="CodeFont"> <item name="android:layout_width">85dp</item> <item name="android:layout_height">wrap_content</item> <item name="android:textColor">#ff0000</item> <item name="android:typeface">monospace</item> <item name="android:textSize">16sp</item> </style> </resources>
定義するのは、style要素となる。item要素のname属性に定義する属性名を書き、item要素の値としてその属性の値を記述する。定義したスタイルは@style/スタイルの名称で使用できる。実際の例で、main.xmlで以下のように使用する。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fff" > <TextView android:text="あいうえおかきくけこさしすせそたちつてとなにぬねの" style="@style/CodeFont" /> </LinearLayout>
なんかもう、XMLの書き方説明書みたいになってきた。