自定义view支持wrap

xiaoxiao2021-02-28  79

问题:在自定义view时,如果直接继承view或者viewgroup的控件,如果不在onMeasure中对wrap_content做处理,

需要重写onMeasure方法,具体如下:

protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec){

super.onMeasure(widthMeasureSpec,heightMeasureSpec);

int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);

int widthSpecSize = MaeasureSpec.getSize(widthMeasureSpec);

int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);

int heightSpecSize = MaeasureSpec.getSize(heightMeasureSpec);

if(widthSpecMode  == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST){

setMeasureedDimension(mWidth,mHeight);

}else if((widthSpecMode  == MeasureSpec.AT_MOST){

setMeasureedDimension(mWidth,heihgtMeasuerSize);

}else if(heightSpecMode == MeasureSpec.AT_MOST){

setMeasureedDimension(widthMeasureSize, mHeihgt);

}

}

其中,mWidth,mHeight为我们给view指定的一个默认的内部宽、高。

转载请注明原文地址: https://www.6miu.com/read-83533.html

最新回复(0)