考虑一下:
样式.xml
<style name="BlueTheme" parent="@android:style/Theme.Black.NoTitleBar">
<item name="theme_color">@color/theme_color_blue</item>
</style>
属性.xml
<attr name="theme_color" format="reference" />
颜色.xml
<color name="theme_color_blue">#ff0071d3</color>
So the 主题色被主题引用。如何以编程方式获取 theme_color (参考)?通常我会使用getResources().getColor()
但在本例中不是这样,因为它被引用了!
这应该可以完成这项工作:
TypedValue typedValue = new TypedValue();
Theme theme = context.getTheme();
theme.resolveAttribute(R.attr.theme_color, typedValue, true);
@ColorInt int color = typedValue.data;
还要确保在调用此代码之前将主题应用到您的活动。要么使用:
android:theme="@style/Theme.BlueTheme"
在您的清单或电话中(在您致电之前setContentView(int)
):
setTheme(R.style.Theme_BlueTheme)
in onCreate()
.
我已经用你的价值观对其进行了测试,效果非常好。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)