Android通过编程将半径设置为ShapeDrawable

Int*_*eep -2 android android-shapedrawable

我可以通过编程方式为ShapeDrawable设置半径吗?

小智 5

您可以这样做:

public static void customView(View v, int backgroundColor, int   borderColor)
{
  GradientDrawable shape = new GradientDrawable();
  shape.setShape(GradientDrawable.RECTANGLE);
  shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });
  shape.setColor(backgroundColor);
  shape.setStroke(3, borderColor);
  v.setBackgroundDrawable(shape);
}
Run Code Online (Sandbox Code Playgroud)

您可以在整个应用程序中使用此功能,并可以选择边框和背景色。