如何从当前主题的自定义布尔属性获取布尔值? [英] How to get boolean from custom boolean attribute for current theme?
问题描述
我在主题中使用布尔格式设置自定义属性.
I am setting custom attribute with Boolean format in my theme.
<attr name="isCompound" format="boolean" />
当我尝试使用以下代码
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.isCompound, typedValue, true);
if(typedValue.data) {
...
}
我收到无法将typedValue.data(int)转换为布尔值的错误.我的问题是如何从当前主题获取布尔值isCompound?
I get error that typedValue.data (int) can not be converted to Boolean. My question is how do I get Boolean value isCompound from the current theme?
推荐答案
感谢@Mike M.的评论
Thanks to @Mike M. for the comment
Blockquote数据字段保存最初指定为" false或" true的0或1. – https://developer.android.com/reference/android/util/TypedValue #TYPE_INT_BOOLEAN
文档说它为true返回1,为false返回0.但是当我测试时,它返回-1表示true,返回0表示false.
Document says that it returns 1 for true and 0 for false. But when I test it returns -1 for true and 0 for false.
我想,安全的方法是检查是否为假.
I guess, safe way would be to check for false.
这篇关于如何从当前主题的自定义布尔属性获取布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!