[Android]修改TextView增加背景

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by TONG on 2017/3/19.
 */

public class MyTextView extends TextView {

    private Paint mPaint1;
    private Paint mPaint2;

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //在构造方法完成必要对象的初始化操作
        mPaint1=new Paint();
        mPaint1.setColor(getResources().getColor(android.R.color.holo_blue_light));
        mPaint1.setStyle(Paint.Style.FILL);

        mPaint2=new Paint();
        mPaint2.setColor(Color.YELLOW);
        mPaint2.setStyle(Paint.Style.FILL);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        //绘制外层矩形
        canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint1);
        //绘制内层矩形
        canvas.drawRect(10,10,getMeasuredWidth()-10,getMeasuredHeight()-10,mPaint2);
        canvas.save();
        //绘制文字前平移10像素
        canvas.translate(10,0);
        //父类完成的方法,即绘制文本
        super.onDraw(canvas);
        canvas.restore();
    }
}
[Android]修改TextView增加背景_第1张图片
Paste_Image.png

你可能感兴趣的:([Android]修改TextView增加背景)