同じコードを何度も繰り返すのは煩わしいという理由だけで、styles.xml 内の多くの重複するエントリを削除したいと考えています。メインテーマがあるとしましょう:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="color_1">#000000</item>
<item name="color_2">#424242</item>
<item name="myMinTextSize">14sp</item>
<item name="myNormalTextSize">18sp</item>
<item name="myMaxTextSize">22sp</item>
<!-- lots of other stuff -->
</style>
attr.xml のサイズ属性は次のとおりです。
<attr name="myMinTextSize" format="dimension" />
<attr name="myNormalTextSize" format="dimension" />
<attr name="myMaxTextSize" format="dimension" />
これで、次のように色を変更するテーマの子がいくつかあります。
<style name="AppTheme.1">
<item name="color_1">#131313</item>
<item name="color_2">#BBBBBB</item>
</style>
テキストのサイズも変更できるようにしたいのですが、そのためには、次のようにテーマごとに子を作成する必要があります。
<style name="AppTheme.Small">
<item name="myMinTextSize">12sp</item>
<item name="myNormalTextSize">14sp</item>
<item name="myMaxTextSize">16sp</item>
</style>
<style name="AppTheme.1.Small">
<item name="myMinTextSize">12sp</item>
<item name="myNormalTextSize">14sp</item>
<item name="myMaxTextSize">16sp</item>
</style>
<!-- and all the other theme children with .Small suffix will have *the exact same thing* -->
色にはたくさんの子供がいて、テキストサイズにはたくさんの子供の子供がいます。私ができるようにしたいのは(プログラムで行う必要なしに)、styles.xml ファイルで同じことを何度も繰り返さないことです。それを行う方法はありますか?たとえば、次の行の何か:
<style name="*.Small">
<item name="myMinTextSize">12sp</item>
<item name="myNormalTextSize">16sp</item>
<item name="myMaxTextSize">20sp</item>
</style>
<style name="*.Large">
<item name="myMinTextSize">16sp</item>
<item name="myNormalTextSize">20sp</item>
<item name="myMaxTextSize">24sp</item>
</style>
ユーザーが次のように選択したオプションに基づいて、プログラムのテーマを選択し、それらをアクティビティに適用します (変数名は、私の主張を理解するのに十分明白であると確信しています)。
mThemeResId = when (mCurrentTheme) {
THEME_0 -> when (mCurrentFontSize) {
SMALL -> R.style.AppTheme_Small
LARGE -> R.style.AppTheme_Large
else -> R.style.AppTheme
}
THEME_1 -> when (mCurrentFontSize) {
SMALL -> R.style.AppTheme_1_Small
LARGE -> R.style.AppTheme_1_Large
else -> R.style.AppTheme_1
}
// and lots of others
}