最終更新:
moonlight_aska 2011年07月23日(土) 18:14:55履歴
スタイルとは, レイアウト属性とその値のセットをいくつか組み合わせて, 1つのIDで使用できるように定義したものである.
スタイルファイルは, res/valuesに配置することで, レイアウト定義ファイルやコードからアクセスできる.
注) コードからはリソースインデックス(R.style.*)としてアクセスできる.

注) コードからはリソースインデックス(R.style.*)としてアクセスできる.
- res/values/style.xml
- style要素(<style>〜</style>)に, style要素のname属性とitem要素(<item>〜</item>)を定義する.
- item要素に, item要素のname属性にレイアウトやウィジェットに指定するXML属性を指定し, 値を設定する.
- 継承する場合には, parent属性に親となるスタイル(@style/親のスタイル名)を設定する.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="FontAttr">
<item name="android:textStyle">italic</item>
<item name="android:textColor">#ffffff</item>
</style>
<style name="SmallFont" parent="@style/FontAttr">
<item name="android:textSize">16sp</item>
</style>
<style name="MiddleFont" parent="@style/FontAttr">
<item name="android:textSize">24sp</item>
</style>
<style name="LargeFont" parent="@style/FontAttr">
<item name="android:textSize">32sp</item>
</style>
</resources>
- res/layout/main.xml
- レイアウトやウィジェットのstyle属性に"@style/スタイル名"を指定することで, 定義したスタイルを適用する.
<?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"
>
<TextView style="@style/SmallFont"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/small_msg"
/>
<TextView style="@style/MiddleFont"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/middle_msg"
/>
<TextView style="@style/LargeFont"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/large_msg"
/>
</LinearLayout>

コメントをかく