Mar*_*arc 5 android android-theme android-styles
我想创建两个主题,GrayTheme和RedTheme,它们修改一个样式属性.例如,这里是我的两个主题,默认字体颜色是白色,这对两个主题都很好:
<style name="RedTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:textColor">@color/white</item>
</style>
<style name="GrayTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:textColor">@color/white</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但是我有一种用于Header TextViews的样式.如果我使用的是RedTheme,我希望样式HeaderFont的textColor为红色,如果是GrayTheme,我希望HeaderFont textColor为灰色,而不必修改访问此HeaderFont样式的各个xml文件.
<style name="HeaderFont" parent="@android:style/TextAppearance.Medium">
<item name="android:textColor">@color/gray</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我一直在寻找一个优雅的解决方案,但一直找不到任何东西.想法?
在找到一个干净的解决方案失败后,我终于找到了一个非常有效的答案.
我创建了一个自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="headerFontColor" format="reference|color" />
</resources>
Run Code Online (Sandbox Code Playgroud)
然后在风格中我添加了自定义属性
<style name="HeaderFont" parent="@android:style/TextAppearance.Medium">
<item name="android:textColor">?headerFontColor</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后我只是将新属性添加到主题中
<style name="RedTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:textColor">@color/white</item>
<item name="headerFontColor">@color/red</item>
</style>
<style name="GrayTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:textColor">@color/white</item>
<item name="headerFontColor">@color/gray</item>
</style>
Run Code Online (Sandbox Code Playgroud)
这个解决方案引导我:根据主题设置颜色
| 归档时间: |
|
| 查看次数: |
1722 次 |
| 最近记录: |