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

爷,独闯天下 提交于 2019-12-17 20:58:09

问题


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 write b.setCornerRadius(3.0f), but it is undefined for button object. Thanks.


回答1:


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>



回答2:


Use GradientDrawable

GradientDrawable gdDefault = new GradientDrawable();
gdDefault.setColor(bgColor);
gdDefault.setCornerRadius(cornerRadius);
gdDefault.setStroke(strokeWidth, strokeColor);



回答3:


See the documentation for Shape Drawable




回答4:


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

GradientDrawable drawable = (GradientDrawable)image.getBackground();
drawable.setGradientRadius(radiuspx);


来源:https://stackoverflow.com/questions/8709595/how-to-set-corner-radiuses-for-the-button-in-java-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!