1

我在 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()
4

1 回答 1

0

您可以在此处读取的属性位于 values\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!!!
}
于 2018-07-13T19:52:36.293 回答