如何从当前主题的自定义布尔属性获取布尔?

问题描述 投票:0回答:1

我正在主题中以布尔格式设置自定义属性。

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

[当我尝试使用以下代码在我的代码中检索此属性时

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

我收到typedValue.data(int)无法转换为布尔值的错误。我的问题是如何从当前主题获取布尔值isCompound?

android material-design android-theme
1个回答
0
投票

感谢@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。

我想,安全的方法是检查错误。

© www.soinside.com 2019 - 2024. All rights reserved.