作者:mobiledu2502875315 | 来源:互联网 | 2023-02-01 20:51
我正在制作一个自定义TextView(Java类),我很难"翻译"该行(在"原始TextView"xml上)
android:background="@drawable/myDrawableShape"
到一个java void来改变"myDrawableShape"的颜色
myDrawableShape.xml
我将从String中获取颜色,以编程方式更改颜色的void(例如)
void colorSet(String color)
提前致谢!
1> Muthukrishna..:
然后,您可以使用下面的代码在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;
}