自定义控件中 wrap_content 属性无效的分析解决

问题

在自定义一个类似锁屏页面时间日期样式的控件,继承 View 的时候,发现在 xml 中使用 wrap_content 属性相当于使用了 match_parent 属性。

原因分析

进入View的源码,可以看到 onMeasure 的方法中

 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
 }

其中看看 getDefaultSize方法,在没有重写 onMeasure 的时候,走如下逻辑

/**
    参数 size :提供的是默认大小
    参数 measureSpec :提供的是测量规格(测量模式&测量大小)
*/
public static int getDefaultSize(int size, int measureSpec) {
        int result = size;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        switch (specMode) {
            case MeasureSpec.UNSPECIFIED: //自定义控件的时候用的比较少
                result = size;
                break;
            case MeasureSpec.AT_MOST: //对应的是 wrap_content 属性
            case MeasureSpec.EXACTLY: //对应的是 match_parent 属性
                result = specSize;
                break;
        }
        return result;
    }

通过上述源码可以知道,在没有特殊情况的处理测量尺寸,自定义继承 View ,属性 wrap_content 与 match_parent 效果一样

解决办法

通过上述分析可知,想要在自定义继承View的时候实现 wrap_content 属性,需要复写 onMeasure 方法。

    //控件默认的宽高
    int defaultWidth = 300; 
    int defaultHeight = 200;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int withSize = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if(getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT
                && getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT){
            setMeasuredDimension(defaultWidth,defaultHeight);
        } else if(getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT){
            setMeasuredDimension(defaultWidth,heightSize);
        } else if(getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT){
            setMeasuredDimension(withSize,defaultHeight);
        }

    }

通过默认的宽高来指定 wrap_content 属性指定的大小。

也可以在 onDraw 的时候计算内容的大小,之后再重新 requestLayout,来实现内容决定控件的大小。

原理引申

整个过程都涉及到 控件的绘制布局的原理流程,推荐看抛物线的系列网站

https://hencoder.com/ui-2-1/

自己用来理解实现的demo地址

https://github.com/qinhaihang/TimeViewDemo

参考的文章

https://blog.csdn.net/carson_ho/article/details/62037760

猜你喜欢

转载自blog.csdn.net/u012102149/article/details/86489511