如何在自定义视图中获取和更改可绘制的背景颜色

时间:2017-11-14 08:59:43

标签: android android-custom-view

我有一个扩展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
    }
}

1 个答案:

答案 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);
    }