以下View可以用Xml加入
public class CzChart_Pie extends View {
Paint paint;
//必须的初始化方法用于代码加入view
public CzChart_Pie(Context context) {
super(context);
initPaint();
}
//必须的的初始化方法用于XML加入view
public CzChart_Pie(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initPaint();
}
//必须初始化画笔
void initPaint(){
paint=new Paint();
paint.setColor(0xFF0000FF);
}
//当代码执行 invalidate() 方法时就会调用该方法重绘视图
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(10,10,100,100,paint);
}
}