继使用typedarray是我styelable.xml无法自定义视图
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MaterialIndicator">
<attr name="mi_indicatorRadius" format="dimension|reference" />
<attr name="mi_indicatorPadding" format="dimension|reference" />
<attr name="mi_indicatorColor" format="color|reference" />
</declare-styleable>
</resources>
林指上述styelable如下,
public MaterialIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
selectedIndicatorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
indicatorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
indicatorPaint.setColor(Color.BLACK);
indicatorPaint.setAlpha((int) (deselectedAlpha * 255));
selectorRect = new RectF();
if (isInEditMode()) {
count = 3;
}
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialIndicator, 0, R.style.MaterialIndicator);
try {
selectedIndicatorPaint.setColor(typedArray.getColor(R.styleable.MaterialIndicator_mi_indicatorColor, 0));
} finally {
typedArray.recycle();
}
}
但它引发以下错误消息
引起:java.lang.UnsupportedOperationException:无法转换为 颜色:类型= 0x1 at android.content.res.TypedArray.getColor(TypedArray.java:339)
我该如何解决这个问题?
如果从XML文件中删除'reference'我的意思是,你只需要的格式会发生什么=”颜色“ – mt0s
仍然是一个问题持续 – Learner
哪一行是339? – mt0s