我知道这是一篇旧帖子,但我认为它在这里很有帮助。不要使用可绘制对象,而是尝试扩展按钮并在画布上绘制椭圆。
开始:
public class YourButton extends Button {
//declare needed variables
private Context context;
private Paint ovalPaint;
public YourButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
this.context = context;
ovalPaint = new Paint();
ovalPaint.setAntiAlias(true);
ovalPaint.setStyle(Paint.Style.FILL);
ovalPaint.setColor(context.getResources().getColor(R.color.YOUR_RED_COLOR);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawOval(getOval(canvas), ovalPaint);
super.onDraw(canvas); //important
}
private RectF getOval(Canvas canvas) {
int offset = (int)(canvas.getWidth() * .05);
return new RectF(0 + offset, 0 + offset, canvas.getWidth() - offset, canvas.getHeight() - offset);
}
}
并将您的 xml 文件设置如下:
<YourButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Text"
android:textSize="30sp"
android:textColor="#fff" />