GradientDrawable 圆角半径不起作用

GradientDrawable corner radius isn't working

我正在使用 GradientDrawable 在矩形上应用圆角半径,这个可绘制对象最终设置在 TextView 上

val drawable = GradientDrawable()
drawable.shape = GradientDrawable.RECTANGLE
drawable.setStroke(3, Color.YELLOW)
drawable.setColor(Color.RED)
drawable.cornerRadius = 10.toFloat()
view.setBackgroundDrawable(drawable)

您是否知道为什么不起作用并且仍然是没有应用角半径的矩形?

我之前试过调用 mutate() 但仍然无法正常工作,也尝试过 cornerRadii 但没有:(

此处 example 了解如何以编程方式创建 GradientDrawable 形状。

GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
shape.setColor(Color.RED);
shape.setStroke(3, Color.YELLOW);

For change the radius for all corners of the gradient.

shape.setCornerRadius(15);

For Change the radius for specific corners of the gradient.

shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });

You can use this drawable as a background as below :

view.setBackgroundDrawable(shape);

更多详情click here