Android 如何以编程方式绘制圆角矩形

Android how to draw Rounded Rectangle shape programmatically

我有 TextView,我想以编程方式绘制圆角矩形形状,我想将该形状设置为 TextView 的背景?我想动态改变它的颜色,我已经发布了图片?

public static void makeRoundCorner(int bgcolor,int radius,View v,int strokeWidth,int strokeColor)
{
    GradientDrawable gdDefault = new GradientDrawable();
    gdDefault.setColor(bgcolor);
    gdDefault.setCornerRadius(radius);
    gdDefault.setStroke(strokeWidth, strokeColor);
    v.setBackgroundDrawable(gdDefault);
}

此处查看 v = 您的文本视图或按钮或任何内容。