自定义view设置默认的宽高

来源:互联网 发布:淘宝旺旺客服模板 编辑:程序博客网 时间:2024/05/17 15:22

重写onMeasure()方法

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int width = measureWidth(widthMeasureSpec);    int height = measureHeight(heightMeasureSpec);    //设置宽高    setMeasuredDimension(width, height);}private int measureWidth(int measureSpec) {    int specMode = MeasureSpec.getMode(measureSpec);    int specSize = MeasureSpec.getSize(measureSpec);    //wrap_content    if (specMode == MeasureSpec.AT_MOST){    }    //fill_parent或者精确值    else if (specMode == MeasureSpec.EXACTLY){    }    return specSize;}//根据xml的设定获取高度private int measureHeight(int measureSpec) {    int specMode = MeasureSpec.getMode(measureSpec);    int specSize = MeasureSpec.getSize(measureSpec);    //wrap_content    if (specMode == MeasureSpec.AT_MOST){    }    //fill_parent或者精确值    else if (specMode == MeasureSpec.EXACTLY){    }    return specSize;}

原创粉丝点击