我正在制作自定义TextView(Java类),并且在“翻译”行时遇到麻烦(在“原始TextView” xml上)

android:background="@drawable/myDrawableShape"

更改为Java void以更改“myDrawableShape”的颜色

myDrawableShape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ffafafaf" />
<corners android:radius="15dp" />

我将从字符串中设置颜色,以编程方式更改颜色的空白可能是(例如)
void colorSet(String color)

提前致谢!

最佳答案

然后,您可以使用以下代码在Java本身中创建Shape Drawable。

public Drawable getRoundRect() {
    RoundRectShape rectShape = new RoundRectShape(new float[]{
            10, 10, 10, 10,
            10, 10, 10, 10
    }, null, null);

    ShapeDrawable shapeDrawable = new ShapeDrawable(rectShape);
    shapeDrawable.getPaint().setColor(Color.parseColor("#FF0000"));
    shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
    shapeDrawable.getPaint().setAntiAlias(true);
    shapeDrawable.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
    return shapeDrawable;
}

08-15 23:49