我知道这是旧的文章,但我认为它可以帮助很多在这里。不要使用drawable,而是尝试扩展按钮并在画布上绘制椭圆。
这里所说:
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" />
感谢您的输入。我试过你的解决方案,它填满了整个按钮。我只想要一个按钮文本周围的形状/背景。和按钮本身的不同背景。 – Manu