自定义View分为三个步骤
onMeasure 测量宽高
onLayout 给空间定位
onDraw 绘制控件
自定义View 的几种实现
1.继承View
完成测量需要注意的细节
int mWidth; int mHeight; @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec,heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode=MeasureSpec.getMode(heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); //是否是warp_content模式 if (widthMode==MeasureSpec.AT_MOST&&heightMode==MeasureSpec.AT_MOST){ setMeasuredDimension(mWidth,mHeight); }else if(widthMode==MeasureSpec.AT_MOST){ setMeasuredDimension(mWidth,heightSize); }else if (heightMode==MeasureSpec.AT_MOST){ setMeasuredDimension(widthSize,mHeight); } }