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

android 自定义View

2016-05-17 09:43 274 查看
自定义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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: