How to create android shape background programmatically?

前端 未结 5 1713
无人共我
无人共我 2020-12-13 03:35

How to create this shape programmatically?




        
5条回答
  •  醉话见心
    2020-12-13 04:20

    You can do it like this:

    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.setBackground(shape);
    }
    

    See the documentation for the meaning of setCornerRadii params.

    You can use this function throughout your app and can put border and background color of your choice.

提交回复
热议问题