更新:在CustomTextView中定义了2个自定义属性。如果在xml中都定义了这两个属性,则可以正常工作。

<com.mycompany.projectname.MyCustomView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:extraColor="?someColor"
/>


这里someColor是另一种颜色属性,因主题而异。
我需要MyCustomView类中的extraColor自定义属性的值...

当前获得它如下:

TypedArray a = context.obtainStyledAttributes(attrs,  R.styleable.CustomTextView, defStyleAttr, 0);


a.getColorStateList()在这里不起作用...

CustomTextView定义为:

<declare-styleable name="CustomTextView">
        <attr name="state" format="boolean" />
        <attr name="extraColor" format="reference|color" />
</declare-styleable>


更新:在CustomTextView中定义了2个自定义属性。
如果两个都在xml中定义,则可以正常工作。

最佳答案

你能试试那个吗..

a.getColor(R.styleable.CustomTextView_extraColor, Color.WHITE)

08-18 17:30
查看更多