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

Ach*_*hin 2 java user-interface android android-layout

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

在此处输入图片说明

Rav*_*avi 5

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);
}
Run Code Online (Sandbox Code Playgroud)

这里 View v = 你的 textview 或按钮或任何东西。