How to set corner radiuses for the button in java code?

前端 未结 4 861
情深已故
情深已故 2020-12-11 05:06

I want to set the rounded corners without xml. How can I do it in java code?

Button b = new Button (this);
b.set???? (??) ;

I tried to wri

相关标签:
4条回答
  • 2020-12-11 05:33

    Use GradientDrawable

    GradientDrawable gdDefault = new GradientDrawable();
    gdDefault.setColor(bgColor);
    gdDefault.setCornerRadius(cornerRadius);
    gdDefault.setStroke(strokeWidth, strokeColor);
    
    0 讨论(0)
  • 2020-12-11 05:35

    See the documentation for Shape Drawable

    0 讨论(0)
  • 2020-12-11 05:36

    Try setGradientRadius(). setCornerRadius() set wrong size.

    GradientDrawable drawable = (GradientDrawable)image.getBackground();
    drawable.setGradientRadius(radiuspx);
    
    0 讨论(0)
  • 2020-12-11 05:57

    create a shape in your drawable folder and set the desired radius and set this drawable as background to your button:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            
        <item>
            <shape android:shape="rectangle">
                <corners android:radius="5dip"/>
            </shape>
        </item>
    </layer-list>
    
    0 讨论(0)
提交回复
热议问题