標籤:target tool str def 計算 add and ecif alt
Android開發中偶爾會用到自訂View,一般情況下,自訂View都需要繼承View類的onMeasure方法,那麼,為什麼要繼承onMeasure()函數呢?什麼情況下要繼承onMeasure()?系統預設的onMeasure()函數行為是怎樣的 ?本文就探究探究這些問題。
首先,我們寫一個自訂View,直接調用系統預設的onMeasure函數,看看會是怎樣的現象:
12345678910111213141516171819202122 |
package com.titcktick.customview; import android.content.Context; import android.util.AttributeSet; import android.view.View; public class CustomView extends View { public CustomView(Context context) { super (context); } public CustomView(Context context, AttributeSet attrs) { super (context, attrs); } @Override protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) { super .onMeasure(widthMeasureSpec, heightMeasureSpec); } } |
1. 父控制項使用match_parent,CustomView使用match_parent
12345678910111213 |
<LinearLayout 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" android:orientation= "vertical" > <com.titcktick.customview.CustomView android:layout_width= "match_parent" android:layout_height= "match_parent" android:layout_margin= "10dp" android:background= "@android:color/black" /> </LinearLayout> |
這裡加了10dp的margin並且把View的背景設定為了黑色,是為了方便辨別我們的CustomView,效果如下:
我們可以看到,預設情況下,如果父控制項和CustomView都使用match_parent,則CustomView會充滿父控制項。
2. 父控制項使用match_parent,CustomView使用wrap_content
把layout檔案中,CustomView的layout_width/layout_height替換為wrap_content,你會發現,結果依然是充滿父控制項。
3. 父控制項使用match_parent,CustomView使用固定的值
把layout檔案中,CustomView的layout_width/layout_height替換為50dp,你會發現,CustomView的顯示結果為50dpx50dp,:
4. 父控制項使用固定的值,CustomView使用match_parent或者wrap_content
那麼,如果把父控制項的layout_width/layout_height替換為50dp,CustomView設定為match_parent或者wrap_content,你會發現,CustomView的顯示結果也是為50dpx50 dp。
5 結論
如果自訂的CustomView採用預設的onMeasure函數,行為如下:
(1) CustomView設定為 match_parent 或者 wrap_content 沒有任何區別,其顯示大小由父控制項決定,它會填充滿整個父控制項的空間。
(2) CustomView設定為固定的值,則其顯示大小為該設定的值。
如果你的自訂控制項的大小計算就是跟系統預設的行為一致的話,那麼你就不需要重寫onMeasure函數了。
6. 怎樣編寫onMeasure函數
系統預設的onMeasure函數的行為就討論到這,下面也說說怎樣重寫onMeasure函數,以及onMeasure函數的基本原理,關鍵區段在代碼中以注釋的形式給出了,僅供參考:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
package com.titcktick.customview; import android.content.Context; import android.util.AttributeSet; import android.view.View; public class CustomView extends View { private static final int DEFAULT_VIEW_WIDTH = 100 ; private static final int DEFAULT_VIEW_HEIGHT = 100 ; public CustomView(Context context) { super (context); } public CustomView(Context context, AttributeSet attrs) { super (context, attrs); } @Override protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) { int width = measureDimension(DEFAULT_VIEW_WIDTH, widthMeasureSpec); int height = measureDimension(DEFAULT_VIEW_HEIGHT, heightMeasureSpec); setMeasuredDimension(width, height); } protected int measureDimension( int defaultSize, int measureSpec ) { int result = defaultSize; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); //1. layout給出了確定的值,比如:100dp //2. layout使用的是match_parent,但父控制項的size已經可以確定了,比如設定的是具體的值或者match_parent if (specMode == MeasureSpec.EXACTLY) { result = specSize; //建議:result直接使用確定值 } //1. layout使用的是wrap_content //2. layout使用的是match_parent,但父控制項使用的是確定的值或者wrap_content else if (specMode == MeasureSpec.AT_MOST) { result = Math.min(defaultSize, specSize); //建議:result不能大於specSize } //UNSPECIFIED,沒有任何限制,所以可以設定任何大小 //多半出現在自訂的父控制項的情況下,期望由自控制項自行決定大小 else { result = defaultSize; } return result; } } |
這樣重載了onMeasure函數之後,你會發現,當CustomView使用match_parent的時候,它會佔滿整個父控制項,而當CustomView使用wrap_content的時候,它的大小則是代碼中定義的預設大小100x100像素。當然,你也可以根據自己的需求改寫measureDimension()的實現。
Android開發實踐:為什麼要繼承onMeasure()