欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

View.onMeasure方法如何在Android中使用

本篇文章為大家展示了View.onMeasure方法如何在Android中使用 ,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

成都創(chuàng)新互聯(lián)公司是一家成都網(wǎng)站設(shè)計、成都做網(wǎng)站,提供網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,網(wǎng)站制作,建網(wǎng)站,按需定制網(wǎng)站,網(wǎng)站開發(fā)公司,公司2013年成立是互聯(lián)行業(yè)建設(shè)者,服務(wù)者。以提升客戶品牌價值為核心業(yè)務(wù),全程參與項目的網(wǎng)站策劃設(shè)計制作,前端開發(fā),后臺程序制作以及后期項目運營并提出專業(yè)建議和思路。

Android View.onMeasure方法詳解及實例

View在屏幕上顯示出來要先經(jīng)過measure(計算)和layout(布局).

1、什么時候調(diào)用onMeasure方法?

當(dāng)控件的父元素正要放置該控件時調(diào)用.父元素會問子控件一個問題,“你想要用多大地方啊?”,然后傳入兩個參數(shù)——widthMeasureSpec和heightMeasureSpec.

這兩個參數(shù)指明控件可獲得的空間以及關(guān)于這個空間描述的元數(shù)據(jù).

更好的方法是你傳遞View的高度和寬度到setMeasuredDimension方法里,這樣可以直接告訴父控件,需要多大地方放置子控件.

接下來的代碼片段給出了如何重寫onMeasure.注意,調(diào)用的本地空方法是來計算高度和寬度的.它們會譯解widthHeightSpec和heightMeasureSpec值,并計算出合適的高度和寬度值.

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

int measuredHeight = measureHeight(heightMeasureSpec);
int measuredWidth = measureWidth(widthMeasureSpec);
setMeasuredDimension(measuredHeight, measuredWidth);
}

private int measureHeight(int measureSpec) {


// Return measured widget height.
}

private int measureWidth(int measureSpec) {

// Return measured widget width.
}

邊界參數(shù)——widthMeasureSpec和heightMeasureSpec ,效率的原因以整數(shù)的方式傳入。在它們使用之前,首先要做的是使用MeasureSpec類的靜態(tài)方法getMode和getSize來譯解,如下面的片段所示:

int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

依據(jù)specMode的值,(MeasureSpec有3種模式分別是UNSPECIFIED, EXACTLY和AT_MOST)

  • 如果是AT_MOST,specSize 代表的是最大可獲得的空間;
  • 如果是EXACTLY,specSize 代表的是精確的尺寸;
  • 如果是UNSPECIFIED,對于控件尺寸來說,沒有任何參考意義。

2、那么這些模式和我們平時設(shè)置的layout參數(shù)fill_parent, wrap_content有什么關(guān)系呢?

經(jīng)過代碼測試就知道,當(dāng)我們設(shè)置width或height為fill_parent時,容器在布局時調(diào)用子 view的measure方法傳入的模式是EXACTLY,因為子view會占據(jù)剩余容器的空間,所以它大小是確定的。

而當(dāng)設(shè)置為 wrap_content時,容器傳進去的是AT_MOST, 表示子view的大小最多是多少,這樣子view會根據(jù)這個上限來設(shè)置自己的尺寸。當(dāng)子view的大小設(shè)置為精確值時,容器傳入的是EXACTLY, 而MeasureSpec的UNSPECIFIED模式目前還沒有發(fā)現(xiàn)在什么情況下使用。 

   View的onMeasure方法默認(rèn)行為是當(dāng)模式為UNSPECIFIED時,設(shè)置尺寸為mMinWidth(通常為0)或者背景drawable的最小尺寸,當(dāng)模式為EXACTLY或者AT_MOST時,尺寸設(shè)置為傳入的MeasureSpec的大小。 

   有個觀念需要糾正的是,fill_parent應(yīng)該是子view會占據(jù)剩下容器的空間,而不會覆蓋前面已布局好的其他view空間,當(dāng)然后面布局子 view就沒有空間給分配了,所以fill_parent屬性對布局順序很重要。以前所想的是把所有容器的空間都占滿了,難怪google在2.2版本里把fill_parent的名字改為match_parent.

在兩種情況下,你必須絕對的處理這些限制。在一些情況下,它可能會返回超出這些限制的尺寸,在這種情況下,你可以讓父元素選擇如何對待超出的View,使用裁剪還是滾動等技術(shù)。

接下來的框架代碼給出了處理View測量的典型實現(xiàn):

java代碼:

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int measuredHeight = measureHeight(heightMeasureSpec);

int measuredWidth = measureWidth(widthMeasureSpec);

setMeasuredDimension(measuredHeight, measuredWidth);

}

private int measureHeight(int measureSpec) {

int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

// Default size if no limits are specified.

int result = 500;
if (specMode == MeasureSpec.AT_MOST){

// Calculate the ideal size of your
// control within this maximum size.
// If your control fills the available
// space return the outer bound.

result = specSize;
}
else if (specMode == MeasureSpec.EXACTLY){

// If your control can fit within these bounds return that value.
result = specSize;
}

return result;
}

private int measureWidth(int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

// Default size if no limits are specified.
int result = 500;
if (specMode == MeasureSpec.AT_MOST){
// Calculate the ideal size of your control
// within this maximum size.
// If your control fills the available space
// return the outer bound.
result = specSize;
}

else if (specMode == MeasureSpec.EXACTLY){
// If your control can fit within these bounds return that value.

result = specSize;
}

return result;
}

上述內(nèi)容就是View.onMeasure方法如何在Android中使用 ,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞名稱:View.onMeasure方法如何在Android中使用
URL標(biāo)題:http://chinadenli.net/article12/gecggc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站收錄品牌網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化小程序開發(fā)、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)