Android 圆角带边框的Button

项目需要,做到这样的效果。搜寻了一下,发现大部分都是在drawable文件夹下新建style.xml

后来查看API,看到 GradientDrawable 类。

所以有了下面的写法:


GradientDrawable drawable = new GradientDrawable();
/**
 * Shape is a rectangle, possibly with rounded corners
 */
drawable.setShape(GradientDrawable.RECTANGLE); // 边框
/**
 * @param width The width in pixels of the stroke
 * @param color The color of the stroke
 */
drawable.setStroke(2, getResources().getColor(R.color.purple)); // 边框粗细及颜色
/**
 * @param argb The color used to fill the shape
 */
drawable.setColor(Color.WHITE); // 颜色
/**
 * @param radius The radius in pixels of the corners of the rectangle shape
 */
drawable.setCornerRadius(6);

btn_ok.setBackgroundDrawable(drawable); 




你可能感兴趣的:(Android例子)