2012/07/17

スタイル/テーマ の名称を .(ドット) で区切るには

スタイル/テーマ の名称を .(ドット) で区切るってどういうこと?

スタイルやテーマは名称を .(ドット) で区切ることができる。
こうすることでスタイルなどを親から継承して階層化していることを意味する。

例えば Android のスタイル定義から抜粋した以下の定義は、

<style name="TextAppearance">
    <item name="android:textColor">?textColorPrimary</item>
    <item name="android:textColorHighlight">#FFFF9200</item>
    <item name="android:textColorHint">?textColorHint</item>
    <item name="android:textColorLink">#5C5CFF</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textStyle">normal</item>
</style>

<style name="TextAppearance.Large">
    <item name="android:textSize">22sp</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textColor">?textColorPrimary</item>
</style>

この定義には、TextAppearanceTextAppearance.Largeの2つが定義されている。
TextAppearanceは以下の設定が定義されている。

Name Value
android:textColor ?textColorPrimary
android:textColorHighlight #FFFF9200
android:textColorHint ?textColorHint
android:textColorLink #5C5CFF
android:textSize 16sp
android:textStyle normal

子スタイルのTextAppearance.LargeTextAppearanceから設定を継承した上で、太字の部分の属性が上書きされる。

Name Value
android:textColor ?textColorPrimary
android:textColorHighlight #FFFF9200
android:textColorHint ?textColorHint
android:textColorLink #5C5CFF
android:textSize 22sp
android:textStyle normal

単にスタイルをグループ化の目的で使うには

グループ単位にアイテムを設定しない空のスタイルを作成することで、グループ化の目的でスタイルを.(ドット)区切りができる。
<style name="Abc"></style>
<style name="Abc.Abc">
    <item name="android:textColor">#abc</item>
</style>
<style name="Abc.Def">
    <item name="android:textColor">#def</item>
</style>
ここで重要なのは Abc という空のスタイルを作成すること。
これがないと Abc.AbcAbc.Def の定義で『親スタイルがない』と怒られる。

0 件のコメント:

コメントを投稿