2
在Android中女,我看到的getColor(INT ID)与ContextCompat.getColor(上下文的背景下,INT ID)所取代。使用ContextCompat.getColor()正确地风格基于主题
我是新来的,在Android的主题造型,所以我不知道如何正确使用此功能。目前,我在我的组织颜色的方式是通过定义,像这样的attrs:
值/ attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="button_bg" format="reference|color"/>
</resources>
然后我引用它们像这样:
值/主题.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowAnimationStyle">@null</item>
</style>
<style name="MyTheme.White">
<item name="button_bg">#fff</item>
</style>
<style name="MyTheme.Black">
<item name="button_bg">#000</item>
</style>
</resources>
This works。 但是,我现在如何在java中获得button_bg
? 我试过ContextCompat.getColor(context, R.attr.button_bg)
但这给了我一个错误Resource not found
。
我接近这个不正确吗?