我正在编辑,使问题更简单,希望有助于得到一个准确的答案。

假设我有如下椭圆形状:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:angle="270"
           android:color="#FFFF0000"/>
    <stroke android:width="3dp"
            android:color="#FFAA0055"/>
</shape>

如何从一个活动类中以编程方式设置颜色?


当前回答

我们可以创建这个kotlin函数。

fun View.updateViewBGSolidColor(colorString: String) {
    when (val background: Drawable = this.background) {
        is ShapeDrawable -> {
            background.paint.color = Color.parseColor(colorString)
        }
        is GradientDrawable -> {
            background.setColor(Color.parseColor(colorString))
        }
        is ColorDrawable -> {
            background.color = Color.parseColor(colorString)
        }
    }
}

然后像下面这样使用它:

yourTextView.updateViewBGSolidColor("#FFFFFF")

其他回答

试试这个:

 public void setGradientColors(int bottomColor, int topColor) {
 GradientDrawable gradient = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]  
 {bottomColor, topColor});
 gradient.setShape(GradientDrawable.RECTANGLE);
 gradient.setCornerRadius(10.f);
 this.setBackgroundDrawable(gradient);
 }

要了解更多细节,请查看这个链接

希望有帮助。

改变自定义绘制的纯色的最好方法是 芬兰湾的科特林。

 (findViewById<TextView>(R.id.testing1).getBackground()).setColorFilter(Color.parseColor("#FFDE03"), PorterDuff.Mode.SRC_IN); 

注意:答案已经更新,以涵盖背景是ColorDrawable实例的场景。谢谢泰勒·普法夫指出这一点。

可绘制对象是一个椭圆形,是ImageView的背景

使用getBackground()从imageView获取Drawable:

Drawable background = imageView.getBackground();

检查通常的嫌疑:

if (background instanceof ShapeDrawable) {
    // cast to 'ShapeDrawable'
    ShapeDrawable shapeDrawable = (ShapeDrawable) background;
    shapeDrawable.getPaint().setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
} else if (background instanceof GradientDrawable) {
    // cast to 'GradientDrawable'
    GradientDrawable gradientDrawable = (GradientDrawable) background;
    gradientDrawable.setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
} else if (background instanceof ColorDrawable) {
    // alpha value may need to be set again after this call
    ColorDrawable colorDrawable = (ColorDrawable) background;
    colorDrawable.setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
}

简洁版:

Drawable background = imageView.getBackground();
if (background instanceof ShapeDrawable) {
    ((ShapeDrawable)background).getPaint().setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
} else if (background instanceof GradientDrawable) {
    ((GradientDrawable)background).setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
} else if (background instanceof ColorDrawable) {
    ((ColorDrawable)background).setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
}

注意,空检查不是必需的。

但是,如果在其他地方使用可绘制对象,则应该在修改它们之前对它们使用mutate()。(默认情况下,从XML加载的可绘制对象共享相同的状态。)

GradientDrawable gd = new GradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM,
        new int[] {0xFF616261,0xFF131313});
gd.setCornerRadius(0f);

layout.setBackgroundDrawable(gd);

这可能会有所帮助

1.最初将形状颜色设置为透明

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
   <solid android:angle="270"
       android:color="@android:color/transparent"/>
   <stroke android:width="3dp"
        android:color="#FFAA0055"/>
</shape>

将形状设置为视图的背景 设置你喜欢的颜色如下: 可绘制bg = view.getBackground(); bg.setColorFilter (Color.parseColor(“#颜色”),PorterDuff.Mode.ADD);