我在Android中有自定义视图,并希望加载attrs和TypedArray在自定义视图上设置的所有属性。我在xml布局中定义了一个backcolor属性,但是无法在构造函数中加载它。
我做了一次尝试但没有成功。
// To be done, get the background color from the attributes.
TypedArray l_typedArray = this.getContext().obtainStyledAttributes(attrs,?? , 0);
// not working Color backColor = l_typedArray.getColor()
您可以在此处阅读的属性位于值\ attrs.xml中的attrs.xml中 - 可以如下所示
<resources>
<declare-styleable name="bubbleview">
<!-- Color of the playground -->
<attr name="playgroundcolor" format="reference|color" />
<!-- Color of the canon arrow coming out of the top ball -->
<attr name="canoncolor" format="reference|color" />
<!-- Color of the score text -->
<attr name="scorecolor" format="reference|color"/>
</declare-styleable>
</resources>
enter code here
{
TypedArray l_typedArray =
getContext().obtainStyledAttributes(attrs,R.styleable.bubbleview);
m_scorecolor = l_typedArray.getColor(R.styleable.bubbleview_scorecolor,Color.RED);
m_canoncolor = l_typedArray.getColor(R.styleable.bubbleview_canoncolor,Color.BLUE);
l_typedArray.recycle(); // never forget this one!!!
}