我试图以编程方式获得主题特定值,如:
MainActivity.getInstance()?.theme?.resolveAttribute(R.attr.settingsTint, value, true)
value.data // sometimes its -1
问题是,有时返回的值是-1,而属性确实存在于attr1 .xml中,并且在style.xml中使用。其他属性也会返回正确的资源id值。
有谁知道是什么问题吗?attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Theme">
.
.
.
<attr name="settingsTint" format="color"/>
.
.
.
</declare-styleable>
</resources>
style.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Theme.AppCompat.Light.NoActionBar-->
<style name="AppTheme" parent="Theme.MaterialComponents">
<item name="settingsTint">@color/settingsTint</item>
</style>
<style name="AppTheme.Dark" parent="AppTheme">
<item name="settingsTint">@color/settingsTint_dark</item>
</style>
</resources>
清单:
<application
android:theme="@style/AppTheme"
<activity
android:theme="@style/AppTheme">
在更改主题之前的某个时刻,我检查了值MainActivity.getInstance()?.theme?.resolveAttribute(R.attr.settingsTint, value, true)
如我之前所说,我看到-1
谢谢!
也许你可以在使用TypedValue.type
之前检查一下。
if (tv.type == TypedValue.TYPE_STRING){
...
}else if(tv.type == TypedValue.NULL){
...
}