您的位置:首页 > 移动开发 > Android开发

自定义switch控件

2015-12-14 18:21 555 查看
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();

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android