Android自定义View之继承自View后的wrap_content属性不生效的解决

关于自定义View,建议大家最好继承自Android已经实现过的View,比如ImageView和TextView,这样比较简单,而且各种属性都能生效,比如match_parent和wrap_content等属性。
但是当你开发的自定义View是继承自View的话,那就需要考虑自己实现wrap_content属性了,如果不自己实现的话,你的wrap_content和match_parent是一样的效果。

关于wrap_content不生效的原因,要从源码来分析onMeasure的过程

假设布局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/> 高度是wrap_content,但是却填充了父容器

</FrameLayout>

android.view.View的测量源码

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

    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:
        case MeasureSpec.EXACTLY:
            result = specSize; // 这里是问题的关键
            break;
        }
        return result;
    }

从上面的代码可以看出,android.view.View在measure时,FrameLayout会传入MeasureSpec.AT_MOST(为什么会出入AT_MOST呢,这里不做分析),但是View居然不关注自身的属性,而是一律使用FrameLayout给的最大可用高度specSize,这就导致了View填充FrameLayout的现象。

那再来看看一个TextView是如何measure的

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content" 宽度是wrap_content,此时宽度真的为0px
        android:layout_height="match_parent"/> 

</FrameLayout>

android.view.TextView的部分代码


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        ......
        if (widthMode == MeasureSpec.EXACTLY) {
            // Parent has told us how big to be. So be it.
            width = widthSize;
        } else {
            ......
            // 根据内容计算出width
            width = Math.max(width, getSuggestedMinimumWidth());
            if (widthMode == MeasureSpec.AT_MOST) {
                width = Math.min(widthSize, width); // 在AT_MOST的情况下,并不是直接使用最大specSize
            }
        }
        ......

继承自View的wrap_content属性的measure解决方案。


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

        ViewGroup.LayoutParams layoutParams = getLayoutParams();

        int width = measureSelfWidthOrHeight(MeasureSpec.getMode(widthMeasureSpec),
                MeasureSpec.getSize(widthMeasureSpec),
                getPaddingLeft() + getPaddingRight(),
                layoutParams.width, getSuggestedMinimumWidth());

        int height = measureSelfWidthOrHeight(MeasureSpec.getMode(heightMeasureSpec),
                MeasureSpec.getSize(heightMeasureSpec),
                getPaddingTop() + getPaddingBottom(),
                layoutParams.height, getSuggestedMinimumHeight());

        setMeasuredDimension(width, height);

        for (int i = 0; i < getChildCount(); i++) {
            if (getChildAt(i).getVisibility() != GONE) {
                getChildAt(i).measure(MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(width), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(height), MeasureSpec.EXACTLY));
            }
        }
    }

    private int measureSelfWidthOrHeight(int heightMode, int heightSize, int extraHeight, int layoutParamHeight, int suggestedMinHeight) {
        int height = 0;
        switch (heightMode) {
            case MeasureSpec.EXACTLY: // 高度是确定的
                height = heightSize;
                break;
            case MeasureSpec.AT_MOST: // AT_MOST一般是因为设置了wrap_content属性获得,但不全是这样,所以的全面考虑layoutParams的3种不同情况
                if (layoutParamHeight == LayoutParams.WRAP_CONTENT) {
                    int disert = Math.max(suggestedMinHeight, extraHeight);
                    height = Math.min(disert, heightSize);
                } else if (layoutParamHeight == LayoutParams.MATCH_PARENT) {
                    height = heightSize;
                } else {
                    height = Math.min(layoutParamHeight + extraHeight, heightSize);
                }
                break;
            case MeasureSpec.UNSPECIFIED:
                if (layoutParamHeight == LayoutParams.WRAP_CONTENT || layoutParamHeight == LayoutParams.MATCH_PARENT) {
                    height = Math.max(suggestedMinHeight, extraHeight);
                } else {
                    height = layoutParamHeight + extraHeight;
                }
                break;
            default:
        }
        return height;
    }

MeasureSpecMode

关于MeasureSpecMode的3种状态:UNSPECIFIED,EXACTLY,AT_MOST。
常使用的有EXACTLY和AT_MOST。

  • EXACTLY这种情形是子View设置了明确的大小时,父容器直接告诉子View一个大小。
  • AT_MOST 在父容器的大小确定,但子View设置了wrap_content时,子View将不能超过父容器的大小时,即可传递该模式。
  • UNSPECIFIED 子View想多大就多大,这在某些wrap_content的View中是存在的,内容大小可以超过父容器,只是在屏幕上不能展示,但是通过getMeasuredWidth()获取的大小是超过父容器的。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章