Android 自定義View:為什么你設(shè)置的wrap_content不起作用?


前言

  • 自定義View是Android開(kāi)發(fā)中非常常用的知識(shí)
  • 可是,在使用過(guò)程中,有些開(kāi)發(fā)者會(huì)發(fā)現(xiàn):為什么自定義View 中設(shè)置的wrap_content屬性不起作用(與match_parent相同作用)?
  • 今天,我將全面分析上述問(wèn)題并給出解決方案。

Carson帶你學(xué)Android自定義View文章系列:
Carson帶你學(xué)Android:自定義View基礎(chǔ)
Carson帶你學(xué)Android:一文梳理自定義View工作流程
Carson帶你學(xué)Android:自定義View繪制準(zhǔn)備-DecorView創(chuàng)建
Carson帶你學(xué)Android:自定義View Measure過(guò)程
Carson帶你學(xué)Android:自定義View Layout過(guò)程
Carson帶你學(xué)Android:自定義View Draw過(guò)程
Carson帶你學(xué)Android:手把手教你寫一個(gè)完整的自定義View
Carson帶你學(xué)Android:Canvas類全面解析
Carson帶你學(xué)Android:Path類全面解析


目錄

示意圖

1. 問(wèn)題描述

在使用自定義View時(shí),View寬 / 高的wrap_content屬性不起自身應(yīng)有的作用,而且是起到與match_parent相同作用。

wrap_contentmatch_parent區(qū)別:

  1. wrap_content:視圖的寬/高被設(shè)定成剛好適應(yīng)視圖內(nèi)容的最小尺寸
  2. match_parent:視圖的寬/高被設(shè)置為充滿整個(gè)父布局
    (在Android API 8之前叫作fill_parent)

其實(shí)這里有兩個(gè)問(wèn)題:

  • 問(wèn)題1:wrap_content屬性不起自身應(yīng)有的作用
  • 問(wèn)題2:wrap_content起到與match_parent相同的作用

2. 知識(shí)儲(chǔ)備

請(qǐng)分析 & 解決問(wèn)題之前,請(qǐng)先看自定義View原理中(2)自定義View Measure過(guò)程 - 最易懂的自定義View原理系列


3. 問(wèn)題分析

問(wèn)題出現(xiàn)在View的寬 / 高設(shè)置,那我們直接來(lái)看自定義View繪制中第一步對(duì)View寬 / 高設(shè)置的過(guò)程:measure過(guò)程中的onMeasure()方法

onMeasure()

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
//參數(shù)說(shuō)明:View的寬 / 高測(cè)量規(guī)格

//setMeasuredDimension()  用于獲得View寬/高的測(cè)量值
//這兩個(gè)參數(shù)是通過(guò)getDefaultSize()獲得的
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),  
           getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));  
}

繼續(xù)往下看getDefaultSize()

getDefaultSize()

  • 作用:根據(jù)View寬/高的測(cè)量規(guī)格計(jì)算View的寬/高值
  • 源碼分析如下:
public static int getDefaultSize(int size, int measureSpec) {  

//參數(shù)說(shuō)明:
// 第一個(gè)參數(shù)size:提供的默認(rèn)大小
// 第二個(gè)參數(shù):寬/高的測(cè)量規(guī)格(含模式 & 測(cè)量大小)

    //設(shè)置默認(rèn)大小
    int result = size; 

    //獲取寬/高測(cè)量規(guī)格的模式 & 測(cè)量大小
    int specMode = MeasureSpec.getMode(measureSpec);  
    int specSize = MeasureSpec.getSize(measureSpec);  

    switch (specMode) {  
        // 模式為UNSPECIFIED時(shí),使用提供的默認(rèn)大小
        // 即第一個(gè)參數(shù):size 
        case MeasureSpec.UNSPECIFIED:  
            result = size;  
            break;  
        // 模式為AT_MOST,EXACTLY時(shí),使用View測(cè)量后的寬/高值
        // 即measureSpec中的specSize
        case MeasureSpec.AT_MOST:  
        case MeasureSpec.EXACTLY:  
            result = specSize;  
            break;  
    }  

 //返回View的寬/高值
    return result;  
}

從上面發(fā)現(xiàn):

  • getDefaultSize()的默認(rèn)實(shí)現(xiàn)中,當(dāng)View的測(cè)量模式是AT_MOST或EXACTLY時(shí),View的大小都會(huì)被設(shè)置成子View MeasureSpec的specSize。
  • 因?yàn)锳T_MOST對(duì)應(yīng)wrap_content;EXACTLY對(duì)應(yīng)match_parent,所以,默認(rèn)情況下,wrap_contentmatch_parent是具有相同的效果的。

解決了問(wèn)題2:wrap_content起到與match_parent相同的作用

那么有人會(huì)問(wèn):wrap_content和match_parent具有相同的效果,為什么是填充父容器的效果呢?

  • 由于在getDefaultSize()的默認(rèn)實(shí)現(xiàn)中,當(dāng)View被設(shè)置成wrap_contentmatch_parent時(shí),View的大小都會(huì)被設(shè)置成子View MeasureSpec的specSize。
  • 所以,這個(gè)問(wèn)題的關(guān)鍵在于子View MeasureSpec的specSize的值是多少

我們知道,子View的MeasureSpec值是根據(jù)子View的布局參數(shù)(LayoutParams)和父容器的MeasureSpec值計(jì)算得來(lái),具體計(jì)算邏輯封裝在getChildMeasureSpec()里。

接下來(lái),我們看生成子View MeasureSpec的方法:getChildMeasureSpec()的源碼分析:

getChildMeasureSpec()

//作用:
/ 根據(jù)父視圖的MeasureSpec & 布局參數(shù)LayoutParams,計(jì)算單個(gè)子View的MeasureSpec
//即子view的確切大小由兩方面共同決定:父view的MeasureSpec 和 子view的LayoutParams屬性 


public static int getChildMeasureSpec(int spec, int padding, int childDimension) {  

 //參數(shù)說(shuō)明
 * @param spec 父view的詳細(xì)測(cè)量值(MeasureSpec) 
 * @param padding view當(dāng)前尺寸的的內(nèi)邊距和外邊距(padding,margin) 
 * @param childDimension 子視圖的布局參數(shù)(寬/高)

    //父view的測(cè)量模式
    int specMode = MeasureSpec.getMode(spec);     

    //父view的大小
    int specSize = MeasureSpec.getSize(spec);     

    //通過(guò)父view計(jì)算出的子view = 父大小-邊距(父要求的大小,但子view不一定用這個(gè)值)   
    int size = Math.max(0, specSize - padding);  

    //子view想要的實(shí)際大小和模式(需要計(jì)算)  
    int resultSize = 0;  
    int resultMode = 0;  

    //通過(guò)父view的MeasureSpec和子view的LayoutParams確定子view的大小  


    // 當(dāng)父view的模式為EXACITY時(shí),父view強(qiáng)加給子view確切的值
   //一般是父view設(shè)置為match_parent或者固定值的ViewGroup 
    switch (specMode) {  
    case MeasureSpec.EXACTLY:  
        // 當(dāng)子view的LayoutParams>0,即有確切的值  
        if (childDimension >= 0) {  
            //子view大小為子自身所賦的值,模式大小為EXACTLY  
            resultSize = childDimension;  
            resultMode = MeasureSpec.EXACTLY;  

        // 當(dāng)子view的LayoutParams為MATCH_PARENT時(shí)(-1)  
        } else if (childDimension == LayoutParams.MATCH_PARENT) {  
            //子view大小為父view大小,模式為EXACTLY  
            resultSize = size;  
            resultMode = MeasureSpec.EXACTLY;  

        // 當(dāng)子view的LayoutParams為WRAP_CONTENT時(shí)(-2)      
        } else if (childDimension == LayoutParams.WRAP_CONTENT) {  
            //子view決定自己的大小,但最大不能超過(guò)父view,模式為AT_MOST  
            resultSize = size;  
            resultMode = MeasureSpec.AT_MOST;  
        }  
        break;  

    // 當(dāng)父view的模式為AT_MOST時(shí),父view強(qiáng)加給子view一個(gè)最大的值。(一般是父view設(shè)置為wrap_content)  
    case MeasureSpec.AT_MOST:  
        // 道理同上  
        if (childDimension >= 0) {  
            resultSize = childDimension;  
            resultMode = MeasureSpec.EXACTLY;  
        } else if (childDimension == LayoutParams.MATCH_PARENT) {  
            resultSize = size;  
            resultMode = MeasureSpec.AT_MOST;  
        } else if (childDimension == LayoutParams.WRAP_CONTENT) {  
            resultSize = size;  
            resultMode = MeasureSpec.AT_MOST;  
        }  
        break;  

    // 當(dāng)父view的模式為UNSPECIFIED時(shí),父容器不對(duì)view有任何限制,要多大給多大
    // 多見(jiàn)于ListView、GridView  
    case MeasureSpec.UNSPECIFIED:  
        if (childDimension >= 0) {  
            // 子view大小為子自身所賦的值  
            resultSize = childDimension;  
            resultMode = MeasureSpec.EXACTLY;  
        } else if (childDimension == LayoutParams.MATCH_PARENT) {  
            // 因?yàn)楦竩iew為UNSPECIFIED,所以MATCH_PARENT的話子類大小為0  
            resultSize = 0;  
            resultMode = MeasureSpec.UNSPECIFIED;  
        } else if (childDimension == LayoutParams.WRAP_CONTENT) {  
            // 因?yàn)楦竩iew為UNSPECIFIED,所以WRAP_CONTENT的話子類大小為0  
            resultSize = 0;  
            resultMode = MeasureSpec.UNSPECIFIED;  
        }  
        break;  
    }  
    return MeasureSpec.makeMeasureSpec(resultSize, resultMode);  
  • 關(guān)于getChildMeasureSpec()里對(duì)于子View的測(cè)量模式和大小的判斷邏輯有點(diǎn)復(fù)雜;
  • 別擔(dān)心,我已經(jīng)幫大家總結(jié)好。具體子View的測(cè)量模式和大小請(qǐng)看下表:
Paste_Image.png

從上面可以看出,當(dāng)子View的布局參數(shù)使用match_parentwrap_content時(shí):

  • 子View的specMode模式:AT_MOST
  • 子View的specSize(寬 / 高):parenSize = 父容器當(dāng)前剩余空間大小 = match_content

4. 問(wèn)題總結(jié)

  • onMeasure()中的getDefaultSize()的默認(rèn)實(shí)現(xiàn)中,當(dāng)View的測(cè)量模式是AT_MOST或EXACTLY時(shí),View的大小都會(huì)被設(shè)置成子View MeasureSpec的specSize。

  • 因?yàn)锳T_MOST對(duì)應(yīng)wrap_content;EXACTLY對(duì)應(yīng)match_parent,所以,默認(rèn)情況下,wrap_contentmatch_parent是具有相同的效果的。

  • 因?yàn)樵谟?jì)算子View MeasureSpec的getChildMeasureSpec()中,子View MeasureSpec在屬性被設(shè)置為wrap_contentmatch_parent情況下,子View MeasureSpec的specSize被設(shè)置成parenSize = 父容器當(dāng)前剩余空間大小

所以:wrap_content起到了和match_parent相同的作用:等于父容器當(dāng)前剩余空間大小


5. 解決方案

當(dāng)自定義View的布局參數(shù)設(shè)置成wrap_content時(shí)時(shí),指定一個(gè)默認(rèn)大小(寬 / 高)。

具體是在復(fù)寫onMeasure()里進(jìn)行設(shè)置

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

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);


    // 獲取寬-測(cè)量規(guī)則的模式和大小
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);

    // 獲取高-測(cè)量規(guī)則的模式和大小
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    // 設(shè)置wrap_content的默認(rèn)寬 / 高值
    // 默認(rèn)寬/高的設(shè)定并無(wú)固定依據(jù),根據(jù)需要靈活設(shè)置
    // 類似TextView,ImageView等針對(duì)wrap_content均在onMeasure()對(duì)設(shè)置默認(rèn)寬 / 高值有特殊處理,具體讀者可以自行查看
    int mWidth = 400;
    int mHeight = 400;

  // 當(dāng)布局參數(shù)設(shè)置為wrap_content時(shí),設(shè)置默認(rèn)值
    if (getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT && getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) {
        setMeasuredDimension(mWidth, mHeight);
    // 寬 / 高任意一個(gè)布局參數(shù)為= wrap_content時(shí),都設(shè)置默認(rèn)值
    } else if (getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT) {
        setMeasuredDimension(mWidth, heightSize);
    } else if (getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) {
        setMeasuredDimension(widthSize, mHeight);
}

這樣,當(dāng)你的自定義View的寬 / 高設(shè)置成wrap_content屬性時(shí)就會(huì)生效了。

特別注意

網(wǎng)上流傳著這么一個(gè)解決方案:

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

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);


    // 獲取寬-測(cè)量規(guī)則的模式和大小
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);

    // 獲取高-測(cè)量規(guī)則的模式和大小
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    // 設(shè)置wrap_content的默認(rèn)寬 / 高值
    // 默認(rèn)寬/高的設(shè)定并無(wú)固定依據(jù),根據(jù)需要靈活設(shè)置
    // 類似TextView,ImageView等針對(duì)wrap_content均在onMeasure()對(duì)設(shè)置默認(rèn)寬 / 高值有特殊處理,具體讀者可以自行查看
    int mWidth = 400;
    int mHeight = 400;

  // 當(dāng)模式是AT_MOST(即wrap_content)時(shí)設(shè)置默認(rèn)值
    if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
        setMeasuredDimension(mWidth, mHeight);
    // 寬 / 高任意一個(gè)模式為AT_MOST(即wrap_content)時(shí),都設(shè)置默認(rèn)值
    } else if (widthMode == MeasureSpec.AT_MOST) {
        setMeasuredDimension(mWidth, heightSize);
    } else if (heightMode == MeasureSpec.AT_MOST) {
        setMeasuredDimension(widthSize, mHeight);
}
  • 上述的解決方案是:通過(guò)判斷測(cè)量模式是否ATMOST從而來(lái)判斷View的參數(shù)是否是wrap_content
  • 可是,通過(guò)下表發(fā)現(xiàn):View的AT_MOST模式對(duì)應(yīng)的不只是wrap_content,也有可能是match_parent

即當(dāng)父View是AT_MOST、View的屬性設(shè)置為match_parent時(shí)

Paste_Image.png
  • 如果還是按照上述的做法,當(dāng)父View為AT_MOST、View為match_parent時(shí),該View的match_parent的效果不就等于wrap_content 嗎?

答:是,當(dāng)父View為AT_MOST、View為match_parent時(shí),該View的match_parent的效果就等于wrap_content 。上述方法存在邏輯錯(cuò)誤,但由于這種情況非常特殊的,所以導(dǎo)致最終的結(jié)果沒(méi)有錯(cuò)誤。具體分析請(qǐng)看下面例子:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cust="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<-- 父View設(shè)為wrap_content,即AT_MOST模式 -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <scut.com.learncustomview.TestMeasureView
          <-- 子View設(shè)為match_parent -->
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</RelativeLayout>
效果圖

從上面的效果可以看出,View大小 = 默認(rèn)值

我再將子View的屬性改為wrap_content

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cust="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<-- 父View設(shè)為wrap_content,即AT_MOST模式 -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <scut.com.learncustomview.TestMeasureView
          <-- 子View設(shè)為wrap_content -->
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>
效果圖

從上面的效果可以看出,View大小還是等于默認(rèn)值。

同上述分析

  • 對(duì)于第一種情況:當(dāng)父View為AT_MOST、View為match_parent時(shí),該View的match_parent的效果就等于wrap_content,上面說(shuō)了這種情況很特殊:父View的大小能剛好包裹子View,子View的大小充滿父View的大小。

也就是說(shuō):父View的大小是看子View的,子View的大小又是看父View的。

  • 那么到底是誰(shuí)看誰(shuí)的大小呢?
    答:
  • 如果沒(méi)設(shè)置默認(rèn)值,就繼續(xù)往上層VIew充滿大小,即從父View的大小等于頂層View的大?。ǎ?,那么子View的大小 = 父View的大小
  • 如果設(shè)置了默認(rèn)值,就用默認(rèn)值。

相信看到這里你已經(jīng)看懂了:

  • 其實(shí)上面說(shuō)的解決方案(通過(guò)判斷測(cè)量模式是否AT_MOST從而來(lái)判斷View的參數(shù)是否是wrap_content)只是在邏輯上表示有些錯(cuò)誤,但從最終結(jié)果上來(lái)說(shuō)是沒(méi)有錯(cuò)的
  • 因?yàn)?strong>當(dāng)父View為AT_MOST、View為match_parent時(shí),該View的match_parent的效果就等于wrap_content
  1. 如果沒(méi)設(shè)置默認(rèn)值,就繼續(xù)往上層VIew充滿大小,即從父View的大小等于頂層View的大小(),那么子View的大小 = 父View的大小
  2. 如果設(shè)置了默認(rèn)值,就用默認(rèn)值。

為了更好的表示判斷邏輯,我建議你們用本文提供的解決方案,即根據(jù)布局參數(shù)判斷默認(rèn)值的設(shè)置


6. 總結(jié)


歡迎關(guān)注Carson_Ho的簡(jiǎn)書

不定期分享關(guān)于安卓開(kāi)發(fā)的干貨,追求短、平、快,但卻不缺深度。


請(qǐng)點(diǎn)贊!因?yàn)槟愕墓膭?lì)是我寫作的最大動(dòng)力

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容