我正在尝试制作自定义视图,并声明了如下样式的属性: -
<resources>
<declare-styleable name="NewCircleView">
<attr name="radius" format="integer"/>
<attr name="circlecolor" format="color"/>
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
在customview的构造函数中,这些值如下所示: -
circleradius=a.getInt(R.styleable.NewCircleView_radius, 0);//global var
circlecolor=a.getColor(R.styleable.NewCircleView_circlecolor, 0);//global var and a is the typed array
Run Code Online (Sandbox Code Playgroud)
通过声明xml如下使用该视图: -
<com.customviews.NewCircleView
android:layout_below="@id/thetext"
android:layout_width="match_parent"
android:layout_height="fill_parent"
app:radius="10000"
app:circlecolor="@color/black"<!--this is defined in colors.xml
/>
Run Code Online (Sandbox Code Playgroud)
在自定义视图中,我将绘制对象设置为: -
thePaintObj.setColor(circlecolor);//circlecolor logs to an integer as expected
Run Code Online (Sandbox Code Playgroud)
我没有得到xml中定义的颜色 - "黑色"
但是,当我将颜色设置为
thePaintObj.setColor(Color.GRAY)
Run Code Online (Sandbox Code Playgroud)
我在视图中得到了颜色
有人能告诉我,我会做错什么?
(注意: - 如果您希望我发布更多代码,请告诉我)
EDIT1: - 发布我的colors.xml.看起来我的代码评论中不清楚: -
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#7f00</color>
<color name="blue">#770000ff</color>
<color name="green">#7700ff00</color>
<color name="yellow">#77ffff00</color>
<color name="black">#000000</color>
</resources>
Run Code Online (Sandbox Code Playgroud)