在ImageButton上面添加文字,可重写Switch
public class CustomerSwitch extends Switch {
private String s1 ;
private String s2;
private Context mContext;
public CustomerSwitch(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
s1 = getResources().getString(R.string.switch_1);
s2 = getResources().getString(R.string.switch_2);
this.setSwitchTextAppearance(mContext, R.style.switch_textColor);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint p = getPaint();
p.setTextSize(20);
p.setColor(Color.RED);
if (!this.isChecked()) {
canvas.drawText(s1, 3 * this.getMeasuredWidth() / 4 - p.measureText(s1) / 2, this.getBaseline(), p);
} else {
canvas.drawText(s2, this.getThumbTextPadding(), this.getBaseline(), p);
}
canvas.restore();
}
}