我正在主题中以布尔格式设置自定义属性。
<attr name="isCompound" format="boolean" />
[当我尝试使用以下代码在我的代码中检索此属性时
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.isCompound, typedValue, true);
if(typedValue.data) {
...
}
我收到typedValue.data(int)无法转换为布尔值的错误。我的问题是如何从当前主题获取布尔值isCompound?
感谢@Mike M.的评论
Blockquote“数据字段保存0或1,最初指定为” false“或” true“。 – https://developer.android.com/reference/android/util/TypedValue#TYPE_INT_BOOLEAN
文档说,对于true,返回1,对于false,返回0。但是当我测试时,它返回-1表示true,返回0表示false。
我想,安全的方法是检查错误。