簡単な問題があります。ユーザーが切り替えることができる複数のテーマを UI に提供したい
問題は、プリミティブ UI クラスのスタイルを設定するテーマを使用したり、XML レイアウト要素で直接スタイルを使用したりできますが、適用したテーマに基づいて変更されるスタイルを定義できないことです。私がやりたいスタイリングのすべてがプリミティブ型に基づいているわけではありません。
私がやりたいことは、CSS セレクターを使用してクラスまたは ID のスタイルを設定するのと似ていますが、テーマでは要素名のスタイルしか設定できません。
私が今できる最善のことは、以下のように、私が構築した実際のスタイルのいずれかを継承する基本スタイルを持つことです。
私の res/values/style.xml
// All styles have to be subclassed here to be made generic, and only one
// can be displayed at a time. Essentially, I'm doing the same thing as setting
// a theme does, but for styles. If I have 50 styles, I have to have 50 of
// these for each theme, and that's not DRY at all!
<style name="MyHeaderStyle" parent="MyHeaderStyle.Default"/>
<!--
<style name="MyHeaderStyle" parent="MyHeaderStyle.Textures"/>
-->
<style name="MyHeaderStyle.Default">
<item name="android:background">@android:color/background_dark</item>
</style>
<style name="MyHeaderStyle.Textures">
<item name="android:background">@drawable/header_texture</item>
</style>
私のlayout.xmlでの使用法:
<!-- Note that I can't use a theme here, because I only want to style a
SPECIFIC element -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/MyHeaderStyle"
android:gravity="center">
</LinearLayout>