getWindowVisibleDisplayFrame()使用總結(jié)

im類(lèi)項(xiàng)目的聊天界面中需要在鍵盤(pán)上顯示一個(gè)輸入控制框,所以需要獲取到軟鍵盤(pán)的高度,這里就需要使用到android中的getWindowVisibleDisplayFrame()方法。收集整理了一些getWindowVisibleDisplayFrame()的相關(guān)資料,這里記錄一下備忘。

getWindowVisibleDisplayFrame()是View類(lèi)下的一個(gè)方法,用來(lái)獲取當(dāng)前窗口可視區(qū)域的大小。該方法原型為:

public void getWindowVisibleDisplayFrame(Rect outRect);

outRect中保存了可視區(qū)域的范圍,如left, top, right, bottom。

該方法使用注意事項(xiàng)
  1. 調(diào)用該方法的view對(duì)象必須在有效的window中,比如activity,fragment或者dialog的layout中。類(lèi)似new TextView(context).getWindowVisibleDisplayFrame(rect)無(wú)法得到正確的結(jié)果。
  2. 該方法必須在view已經(jīng)attach到window時(shí)調(diào)用才能得到期望的正確結(jié)果。比如我們可以在Activity、Fragment和Dialog的onWindowFocusChanged()方法中執(zhí)行,在view的onAttachedToWindow()中可能無(wú)法獲得正確結(jié)果
  3. outRect所表示的只是窗體可見(jiàn)范圍,其會(huì)受到系統(tǒng)狀態(tài)欄,虛擬鍵盤(pán)和導(dǎo)航欄的影響,狀態(tài)欄主要影響outRect的top值,虛擬鍵盤(pán)和導(dǎo)航欄會(huì)影響outRect的bottom值
getWindowVisibleDisplayFrame()的應(yīng)用場(chǎng)景

由于Android系統(tǒng)并沒(méi)有提供api來(lái)獲取系統(tǒng)狀態(tài)欄,軟鍵盤(pán)和導(dǎo)航欄的高度,所以我們通常使用getWindowVisibleDisplayFrame()來(lái)得到這些模塊的值。對(duì)系統(tǒng)狀態(tài)欄高度,獲取一個(gè)非全屏,且窗口的LayoutParams的height設(shè)置為WindowManager.LayoutParams.MATCH_PARENT的窗口可視區(qū)域大小,其top值就是狀態(tài)欄的高度。對(duì)系統(tǒng)軟鍵盤(pán),獲取一個(gè)高度是MATCH_PARENT的窗口在軟鍵盤(pán)顯示和隱藏兩種不同狀態(tài)下的可視區(qū)域大小,將bottom值相減就可以得到軟鍵盤(pán)的高度。對(duì)系統(tǒng)虛擬按鍵欄,獲取一個(gè)高度是MATCH_PARENT的窗口在虛擬按鍵顯示和隱藏兩種不同狀態(tài)下的可視區(qū)域大小,將bottom值相減就可以得到虛擬按鍵的高度。

下面提供獲取狀態(tài)欄,虛擬鍵盤(pán)和導(dǎo)航欄的幾種方法:
public static int getStatusBarHeight(Context context) {
    int statusbarheight = 0;
    try {
            Class<?> c = Class.forName("com.android.internal.R$dimen");
            Object o = c.newInstance();
            Field field = c.getField("status_bar_height");
            int x = (Integer) field.get(o);
            statusbarheight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    return statusbarheight;
}
    
public static int getStatusBarHeight(View view) {
    Rect outRect = new Rect();
    view.getWindowVisibleDisplayFrame(outRect);
    statusbarheight = outRect.top;  
    return outRect.top;
}

/**
* 獲取軟鍵盤(pán)高度
*/
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            Rect rect = new Rect();
            int windowVisibleBottomWithoutKeyboard;
            
            @Override
            public void onGlobalLayout() {
                rect.setEmpty();
                view.getWindowVisibleDisplayFrame(rect);
                int detal = windowVisibleBottomWithoutKeyboard - rect.bottom;
                if (detal > screenHeight / 3) {
                    keyboardHeight = detal;
                } else {
                    windowVisibleBottomWithoutKeyboard = rect.bottom;
                } 
            }
        });
        
public static int getNavBarHeight(Context context){
    if (isMeizu()) {
        return getSmartBarHeight(context);
    }
    if (checkDeviceHasNavigationBar(context)) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            return resources.getDimensionPixelSize(resourceId);
        }
    }
    return 0;
}

public static boolean checkDeviceHasNavigationBar(Context context) {
        boolean hasNavigationBar = false;
        Resources rs = context.getResources();
        int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id);
        }
        
        String navBarOverride = getSystemProperty("qemu.hw.mainkeys", "");
        if ("1".equals(navBarOverride)) {
            hasNavigationBar = false;
        } else if ("0".equals(navBarOverride)) {
            hasNavigationBar = true;
        }
        
        return hasNavigationBar;

}

public static boolean isMeizu() {
    /* 獲取魅族系統(tǒng)操作版本標(biāo)識(shí)*/
    String meizuFlymeOSFlag = getSystemProperty("ro.build.display.id", "");
    if (TextUtils.isEmpty(meizuFlymeOSFlag)) {
        return false;
    } else if (meizuFlymeOSFlag.contains("flyme") || meizuFlymeOSFlag.toLowerCase().contains("flyme")) {
        return true;
    } else {
        return false;
    }
}

public static int getSmartBarHeight(Context context) {
    try {
        Class c = Class.forName("com.android.internal.R$dimen");
        Object obj = c.newInstance();
        Field field = c.getField("mz_action_button_min_height");
        int height = Integer.parseInt(field.get(obj).toString());
        return context.getResources().getDimensionPixelSize(height);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;

參考文檔

Android獲取窗口可視區(qū)域大小: getWindowVisibleDisplayFrame()

?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 獲取窗口可視區(qū)域大小: getWindowVisibleDisplayFrame() 是View類(lèi)下的一個(gè)方法,從...
    Master_Yang閱讀 843評(píng)論 0 0
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,765評(píng)論 25 709
  • 問(wèn)答題47 /72 常見(jiàn)瀏覽器兼容性問(wèn)題與解決方案? 參考答案 (1)瀏覽器兼容問(wèn)題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,093評(píng)論 1 92
  • 演講大舞臺(tái) 很多年以前,我在一家連鎖店工作,一天老板請(qǐng)行業(yè)中的一位老師來(lái)給我們講課。當(dāng)時(shí)所有同事加起...
    17_陸小鳳琴閱讀 225評(píng)論 0 0
  • 六月,一家歡喜一家憂的日子也已經(jīng)過(guò)去,有人實(shí)習(xí),有人繼續(xù)學(xué)習(xí)。選擇去哪里也不過(guò)一個(gè)短暫的決定而已,就像開(kāi)學(xué)的九月,...
    失憶無(wú)效閱讀 428評(píng)論 2 3

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