让编程属性(而不是在一个视图中,却处处) [英] get attribute programmatically (NOT in a view, but everywhere)
本文介绍了让编程属性(而不是在一个视图中,却处处)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我定义的属性(我在我的主题设置)
I've defined an attribute (and I set it in my theme)
<attr name="primaryColor" format="color|reference" />
我怎样才能在code中的颜色?我试过 getResources()的getColor(R.attr.primaryColor)
但是这个失败...
how can I get the color in the code? I tried getResources().getColor(R.attr.primaryColor)
but this fails...
推荐答案
试试这个:
需要一个语境。
Try this:
Need a Context.
TypedValue typeValue = new TypedValue();
context.getTheme().resolveAttribute(R.attr.primaryColor, typeValue, true);
if (typeValue.type >= TypedValue.TYPE_FIRST_COLOR_INT &&
typeValue.type <= TypedValue.TYPE_LAST_COLOR_INT) {
// R.attr.primaryColor is a color
int color = typeValue.data;
} else {
// R.attr.primaryColor is not a color
}
这篇关于让编程属性(而不是在一个视图中,却处处)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文