為什麼你的自定義View wrap_content不起作用?
前言
- 自定義View是Android開發中非常常用的知識
- 可是,在使用過程中,有些開發者會發現:為什麼自定義View 中設定的
wrap_content
屬性不起作用(與match_parent
相同作用)? - 今天,我將全面分析上述問題並給出解決方案。
目錄
1. 問題描述
在使用自定義View時,View寬 / 高的wrap_content
屬性不起自身應有的作用,而且是起到與match_parent
相同作用。
wrap_content
與match_parent
區別:
1.wrap_content
:檢視的寬/高被設定成剛好適應檢視內容的最小尺寸
2.match_parent
:檢視的寬/高被設定為充滿整個父佈局
(在Android API 8之前叫作fill_parent
)
其實這裡有兩個問題:
- 問題1:
wrap_content
屬性不起自身應有的作用 - 問題2:
wrap_content
起到與match_parent
相同的作用
2. 知識儲備
3. 問題分析
問題出現在View的寬 / 高設定,那我們直接來看自定義View繪製中第一步對View寬 / 高設定的過程:measure過程中的onMeasure()
方法
onMeasure()
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//引數說明:View的寬 / 高測量規格
//setMeasuredDimension() 用於獲得View寬/高的測量值
//這兩個引數是通過getDefaultSize()獲得的
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
繼續往下看getDefaultSize()
getDefaultSize()
- 作用:根據View寬/高的測量規格計算View的寬/高值
- 原始碼分析如下:
public static int getDefaultSize(int size, int measureSpec) {
//引數說明:
// 第一個引數size:提供的預設大小
// 第二個引數:寬/高的測量規格(含模式 & 測量大小)
//設定預設大小
int result = size;
//獲取寬/高測量規格的模式 & 測量大小
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
switch (specMode) {
// 模式為UNSPECIFIED時,使用提供的預設大小
// 即第一個引數:size
case MeasureSpec.UNSPECIFIED:
result = size;
break;
// 模式為AT_MOST,EXACTLY時,使用View測量後的寬/高值
// 即measureSpec中的specSize
case MeasureSpec.AT_MOST:
case MeasureSpec.EXACTLY:
result = specSize;
break;
}
//返回View的寬/高值
return result;
}
從上面發現:
- 在
getDefaultSize()
的預設實現中,當View的測量模式是AT_MOST或EXACTLY時,View的大小都會被設定成子View MeasureSpec的specSize。 - 因為AT_MOST對應wrap_content;EXACTLY對應match_parent,所以,預設情況下,
wrap_content
和match_parent
是具有相同的效果的。
解決了問題2:
wrap_content
起到與match_parent
相同的作用
那麼有人會問:wrap_content和match_parent具有相同的效果,為什麼是填充父容器的效果呢?
- 由於在
getDefaultSize()
的預設實現中,當View被設定成wrap_content
和match_parent
時,View的大小都會被設定成子View MeasureSpec的specSize。 - 所以,這個問題的關鍵在於子View MeasureSpec的specSize的值是多少
我們知道,子View的MeasureSpec值是根據子View的佈局引數(LayoutParams)和父容器的MeasureSpec值計算得來,具體計算邏輯封裝在getChildMeasureSpec()裡。
接下來,我們看生成子View MeasureSpec的方法:getChildMeasureSpec()
的原始碼分析:
getChildMeasureSpec()
//作用:
/ 根據父檢視的MeasureSpec & 佈局引數LayoutParams,計算單個子View的MeasureSpec
//即子view的確切大小由兩方面共同決定:父view的MeasureSpec 和 子view的LayoutParams屬性
public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
//引數說明
* @param spec 父view的詳細測量值(MeasureSpec)
* @param padding view當前尺寸的的內邊距和外邊距(padding,margin)
* @param childDimension 子檢視的佈局引數(寬/高)
//父view的測量模式
int specMode = MeasureSpec.getMode(spec);
//父view的大小
int specSize = MeasureSpec.getSize(spec);
//通過父view計算出的子view = 父大小-邊距(父要求的大小,但子view不一定用這個值)
int size = Math.max(0, specSize - padding);
//子view想要的實際大小和模式(需要計算)
int resultSize = 0;
int resultMode = 0;
//通過父view的MeasureSpec和子view的LayoutParams確定子view的大小
// 當父view的模式為EXACITY時,父view強加給子view確切的值
//一般是父view設定為match_parent或者固定值的ViewGroup
switch (specMode) {
case MeasureSpec.EXACTLY:
// 當子view的LayoutParams>0,即有確切的值
if (childDimension >= 0) {
//子view大小為子自身所賦的值,模式大小為EXACTLY
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
// 當子view的LayoutParams為MATCH_PARENT時(-1)
} else if (childDimension == LayoutParams.MATCH_PARENT) {
//子view大小為父view大小,模式為EXACTLY
resultSize = size;
resultMode = MeasureSpec.EXACTLY;
// 當子view的LayoutParams為WRAP_CONTENT時(-2)
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
//子view決定自己的大小,但最大不能超過父view,模式為AT_MOST
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;
// 當父view的模式為AT_MOST時,父view強加給子view一個最大的值。(一般是父view設定為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;
// 當父view的模式為UNSPECIFIED時,父容器不對view有任何限制,要多大給多大
// 多見於ListView、GridView
case MeasureSpec.UNSPECIFIED:
if (childDimension >= 0) {
// 子view大小為子自身所賦的值
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// 因為父view為UNSPECIFIED,所以MATCH_PARENT的話子類大小為0
resultSize = 0;
resultMode = MeasureSpec.UNSPECIFIED;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// 因為父view為UNSPECIFIED,所以WRAP_CONTENT的話子類大小為0
resultSize = 0;
resultMode = MeasureSpec.UNSPECIFIED;
}
break;
}
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
- 關於getChildMeasureSpec()裡對於子View的測量模式和大小的判斷邏輯有點複雜;
- 別擔心,我已經幫大家總結好。具體子View的測量模式和大小請看下錶:
從上面可以看出,當子View的佈局引數使用wrap_content
或wrap_content
時:
- 子View的specMode模式:AT_MOST
- 子View的specSize(寬 / 高):parenSize = 父容器當前剩餘空間大小 = match_content
4. 問題總結
- 在
onMeasure()中的getDefaultSize()
的預設實現中,當View的測量模式是AT_MOST或EXACTLY時,View的大小都會被設定成子View MeasureSpec的specSize。 因為AT_MOST對應
wrap_content
;EXACTLY對應match_parent
,所以,預設情況下,wrap_content
和match_parent
是具有相同的效果的。因為在計運算元View MeasureSpec的
getChildMeasureSpec()
中,子View MeasureSpec在屬性被設定為wrap_content
或match_parent
情況下,子View MeasureSpec的specSize被設定成parenSize = 父容器當前剩餘空間大小
所以:wrap_content
起到了和match_parent
相同的作用:等於父容器當前剩餘空間大小
5. 解決方案:
當自定義View的佈局引數設定成wrap_content時時,指定一個預設大小(寬 / 高)。
具體是在複寫
onMeasure()
裡進行設定
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 獲取寬-測量規則的模式和大小
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
// 獲取高-測量規則的模式和大小
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
// 設定wrap_content的預設寬 / 高值
// 預設寬/高的設定並無固定依據,根據需要靈活設定
// 類似TextView,ImageView等針對wrap_content均在onMeasure()對設定預設寬 / 高值有特殊處理,具體讀者可以自行檢視
int mWidth = 400;
int mHeight = 400;
// 當佈局引數設定為wrap_content時,設定預設值
if (getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT && getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) {
setMeasuredDimension(mWidth, mHeight);
// 寬 / 高任意一個佈局引數為= wrap_content時,都設定預設值
} else if (getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT) {
setMeasuredDimension(mWidth, heightSize);
} else if (getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) {
setMeasuredDimension(widthSize, mHeight);
}
這樣,當你的自定義View的寬 / 高設定成wrap_content屬性時就會生效了。
特別注意
網上流傳著這麼一個解決方案:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 獲取寬-測量規則的模式和大小
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
// 獲取高-測量規則的模式和大小
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
// 設定wrap_content的預設寬 / 高值
// 預設寬/高的設定並無固定依據,根據需要靈活設定
// 類似TextView,ImageView等針對wrap_content均在onMeasure()對設定預設寬 / 高值有特殊處理,具體讀者可以自行檢視
int mWidth = 400;
int mHeight = 400;
// 當模式是AT_MOST(即wrap_content)時設定預設值
if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(mWidth, mHeight);
// 寬 / 高任意一個模式為AT_MOST(即wrap_content)時,都設定預設值
} else if (widthMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(mWidth, heightSize);
} else if (heightMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(widthSize, mHeight);
}
- 上述的解決方案是:通過判斷測量模式是否ATMOST從而來判斷View的引數是否是
wrap_content
- 可是,通過下表發現:View的
AT_MOST
模式對應的不只是wrap_content
,也有可能是match_parent
即當父View是
AT_MOST
、View的屬性設定為match_parent
時
- 如果還是按照上述的做法,當父View為
AT_MOST
、View為match_parent
時,該View的match_parent
的效果不就等於wrap_content
嗎?
答:是,當父View為AT_MOST
、View為match_parent
時,該View的match_parent
的效果就等於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設為wrap_content,即AT_MOST模式 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<scut.com.learncustomview.TestMeasureView
<-- 子View設為match_parent -->
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
從上面的效果可以看出,View大小 = 預設值
我再將子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設為wrap_content,即AT_MOST模式 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<scut.com.learncustomview.TestMeasureView
<-- 子View設為wrap_content -->
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
從上面的效果可以看出,View大小還是等於預設值。
同上述分析
對於第一種情況:當父View為
AT_MOST
、View為match_parent
時,該View的match_parent
的效果就等於wrap_content
,上面說了這種情況很特殊:父View的大小能剛好包裹子View,子View的大小充滿父View的大小。也就是說:父View的大小是看子View的,子View的大小又是看父View的。
那麼到底是誰看誰的大小呢?
答:- 如果沒設定預設值,就繼續往上層VIew充滿大小,即從父View的大小等於頂層View的大小(),那麼子View的大小 = 父View的大小
- 如果設定了預設值,就用預設值。
相信看到這裡你已經看懂了:
- 其實上面說的解決方案(通過判斷測量模式是否
AT_MOST
從而來判斷View的引數是否是wrap_content
)只是在邏輯上表示有些錯誤,但從最終結果上來說是沒有錯的 - 因為當父View為
AT_MOST
、View為match_parent
時,該View的match_parent
的效果就等於wrap_content
- 如果沒設定預設值,就繼續往上層VIew充滿大小,即從父View的大小等於頂層View的大小(),那麼子View的大小 = 父View的大小
- 如果設定了預設值,就用預設值。
為了更好的表示判斷邏輯,我建議你們用本文提供的解決方案,即根據佈局引數判斷預設值的設定