继承自view的自定义控件的wrap_content和padding两个属性不会生效,解决方法/**
* 自定义一个圆形
* @author luoshen
* 2016年11月29日下午1:28:04
*/
public class CircleView extends View{
private int mColor = Color.RED;
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CircleView(Context context) {
super(context);
init();
}
private void init(){
mPaint.setColor(mColor);
}
/**
* 此方法中的代码是为了让wrap_content属性生效,并给此属性设置宽高
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//取出宽度的测量模式
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
//取出宽度的确切数值
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
//取出高度的测量模式
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
//取出高度的确切数值
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthSpecMode == MeasureSpec.AT_MOST && heightMeasureSpec == MeasureSpec.AT_MOST) {
//设置wrap_content属性的宽高
setMeasuredDimension(200, 200);
}else if (widthSpecMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(200, heightSpecSize);
}else if (heightSpecMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(widthSpecSize, 200);
}
}
/**
* 此方法内部的代码是为了让padding属性生效
*
* 并且画了一个圆
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
int paddingTop = getPaddingTop();
int paddingBottom = getPaddingBottom();
//减去padding值
int width = getWidth() - paddingLeft - paddingRight;
int height = getHeight() - paddingBottom - paddingTop;
int radius = Math.min(width,height)/2;
canvas.drawCircle(paddingLeft+width/2, paddingTop+height/2, radius, mPaint);
}
}
/**
* 自定义一个圆形
* @author luoshen
* 2016年11月29日下午1:28:04
*/
public class CircleView extends View{
private int mColor = Color.RED;
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CircleView(Context context) {
super(context);
init();
}
private void init(){
mPaint.setColor(mColor);
}
/**
* 此方法中的代码是为了让wrap_content属性生效,并给此属性设置宽高
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//取出宽度的测量模式
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
//取出宽度的确切数值
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
//取出高度的测量模式
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
//取出高度的确切数值
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthSpecMode == MeasureSpec.AT_MOST && heightMeasureSpec == MeasureSpec.AT_MOST) {
//设置wrap_content属性的宽高
setMeasuredDimension(200, 200);
}else if (widthSpecMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(200, heightSpecSize);
}else if (heightSpecMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(widthSpecSize, 200);
}
}
/**
* 此方法内部的代码是为了让padding属性生效
*
* 并且画了一个圆
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
int paddingTop = getPaddingTop();
int paddingBottom = getPaddingBottom();
//减去padding值
int width = getWidth() - paddingLeft - paddingRight;
int height = getHeight() - paddingBottom - paddingTop;
int radius = Math.min(width,height)/2;
canvas.drawCircle(paddingLeft+width/2, paddingTop+height/2, radius, mPaint);
}
}