How to get boolean from custom boolean attribute for current theme?

依然范特西╮ 提交于 2020-03-25 19:27:04

问题


I am setting custom attribute with Boolean format in my theme.

<attr name="isCompound" format="boolean" />

When I am trying to retrieve this attribute in my code using following

TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.isCompound, typedValue, true);
if(typedValue.data) {
    ...
}

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?


回答1:


Thanks to @Mike M. for the comment

Blockquote "The data field holds 0 or 1 that was originally specified as "false" or "true"." – https://developer.android.com/reference/android/util/TypedValue#TYPE_INT_BOOLEAN

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.



来源:https://stackoverflow.com/questions/60667113/how-to-get-boolean-from-custom-boolean-attribute-for-current-theme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!