繼使用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