我有一个扩展TextView的自定义视图,背景是可绘制的,
<CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/circile_background" />
并希望在自定义视图中更改背景颜色
public class CustomView extends TextView {
TypedArray typedArray;
public CustomView (Context context) {
super(context);
}
public CustomView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setColorLevel(int color) {
// update color here
}
}
答案 0 :(得分:1)
在此处查看类似问题How to change colors of a Drawable in Android?
public void setDangerLevel(int color) {
Drawable drawable=this.getBackground().getCurrent();
drawable.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
}