日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android開發(fā)實(shí)踐:為什么要繼承onMeasure()-創(chuàng)新互聯(lián)

Android開發(fā)中偶爾會(huì)用到自定義View,一般情況下,自定義View都需要繼承View類的onMeasure方法,那么,為什么要繼承onMeasure()函數(shù)呢?什么情況下要繼承onMeasure()?系統(tǒng)默認(rèn)的onMeasure()函數(shù)行為是怎樣的 ?本文就探究探究這些問題。

成都創(chuàng)新互聯(lián)主要企業(yè)基礎(chǔ)官網(wǎng)建設(shè),電商平臺建設(shè),移動(dòng)手機(jī)平臺,小程序開發(fā)等一系列專為中小企業(yè)定制設(shè)計(jì)產(chǎn)品體系;應(yīng)對中小企業(yè)在互聯(lián)網(wǎng)運(yùn)營的各種問題,為中小企業(yè)在互聯(lián)網(wǎng)的運(yùn)營中保駕護(hù)航。

首先,我們寫一個(gè)自定義View,直接調(diào)用系統(tǒng)默認(rèn)的onMeasure函數(shù),看看會(huì)是怎樣的現(xiàn)象:

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



    

這里加了10dp的margin并且把View的背景設(shè)置為了黑色,是為了方便辨別我們的CustomView,效果如下:

Android開發(fā)實(shí)踐:為什么要繼承onMeasure()

我們可以看到,默認(rèn)情況下,如果父控件和CustomView都使用match_parent,則CustomView會(huì)充滿父控件。

2.  父控件使用match_parent,CustomView使用wrap_content

把layout文件中,CustomView的layout_width/layout_height替換為wrap_content,你會(huì)發(fā)現(xiàn),結(jié)果依然是充滿父控件。

3.  父控件使用match_parent,CustomView使用固定的值

把layout文件中,CustomView的layout_width/layout_height替換為50dp,你會(huì)發(fā)現(xiàn),CustomView的顯示結(jié)果為50dpx50dp,如圖所示:

Android開發(fā)實(shí)踐:為什么要繼承onMeasure()

4.  父控件使用固定的值,CustomView使用match_parent或者wrap_content

那么,如果把父控件的layout_width/layout_height替換為50dp,CustomView設(shè)置為match_parent或者wrap_content,你會(huì)發(fā)現(xiàn),CustomView的顯示結(jié)果也是為50dpx50 dp。

5  結(jié)論

如果自定義的CustomView采用默認(rèn)的onMeasure函數(shù),行為如下:

(1) CustomView設(shè)置為 match_parent 或者 wrap_content 沒有任何區(qū)別,其顯示大小由父控件決定,它會(huì)填充滿整個(gè)父控件的空間。

(2) CustomView設(shè)置為固定的值,則其顯示大小為該設(shè)定的值。

如果你的自定義控件的大小計(jì)算就是跟系統(tǒng)默認(rèn)的行為一致的話,那么你就不需要重寫onMeasure函數(shù)了。

6. 怎樣編寫onMeasure函數(shù)

系統(tǒng)默認(rèn)的onMeasure函數(shù)的行為就討論到這,下面也說說怎樣重寫onMeasure函數(shù),以及onMeasure函數(shù)的基本原理,關(guān)鍵部分在代碼中以注釋的形式給出了,僅供參考:

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已經(jīng)可以確定了,比如設(shè)置的是具體的值或者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,沒有任何限制,所以可以設(shè)置任何大小
        //多半出現(xiàn)在自定義的父控件的情況下,期望由自控件自行決定大小
        else {      
            result = defaultSize; 
        }
        
        return result;
    }
}

這樣重載了onMeasure函數(shù)之后,你會(huì)發(fā)現(xiàn),當(dāng)CustomView使用match_parent的時(shí)候,它會(huì)占滿整個(gè)父控件,而當(dāng)CustomView使用wrap_content的時(shí)候,它的大小則是代碼中定義的默認(rèn)大小100x100像素。當(dāng)然,你也可以根據(jù)自己的需求改寫measureDimension()的實(shí)現(xiàn)。

關(guān)于onMeasure的討論就介紹到這兒了,有任何疑問歡迎留言或者來信lujun.hust@gmail.com交流,或者關(guān)注我的新浪微博 @盧_俊 獲取最新的文章和資訊。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。


新聞標(biāo)題:Android開發(fā)實(shí)踐:為什么要繼承onMeasure()-創(chuàng)新互聯(lián)
轉(zhuǎn)載來源:http://www.dlmjj.cn/article/ehcoc.html