看見網上一堆沒有處理沉浸式狀態欄以及虛擬按鍵的錯誤處理類,這裏給出我實踐過的代碼android
package com.zjw.apporder;
import android.app.Activity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
/**
* 正確解決webview不能adjustResize的處理類(包含了沉浸式狀態欄以及虛擬按鍵等狀況的適配設置)
*/
public class AndroidBug5497Workaround {
public static void assistActivity(Activity activity) {
new AndroidBug5497Workaround(activity);
}
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private Activity activity;
private int statusBarHeight;
private AndroidBug5497Workaround(Activity activity) {
//獲取狀態欄的高度
int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
this.activity = activity;
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
//界面出現變更都會調用這個監聽事件
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = (FrameLayout.LayoutParams)
mChildOfContent.getLayoutParams();
}
private boolean mIsFirstResize = true;
private int mChildOfContentSize = 0;
private int usableHeightNow;
private void possiblyResizeChildOfContent() {
//錯誤作法拿到usableHeightNow
// int usableHeightNow = computeUsableHeight();
if (mIsFirstResize) {
//保留原來系統默認算好的mChildOfContent高度(根據flag和系統狀態欄和下面虛擬按鍵和諧相處的正確值)
mChildOfContentSize = mChildOfContent.getHeight();
mIsFirstResize = false;
}
usableHeightNow = mChildOfContent.getHeight();
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard / 4)) {
// keyboard probably just became visible
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
} else {
// keyboard probably just became hidden
//這裏不能單純使用frameLayoutParams.height = computeUsableHeight(); 由於usableHeightNow和一些flag有關,網上frameLayoutParams.height = usableHeightSansKeyboard 直接就錯了,由於usableHeightSansKeyboard恆爲全屏高度
//1.當使用 window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN) 全屏時getWindowVisibleDisplayFrame高度爲全屏(系統狀態欄和下面虛擬按鍵呼出懸浮在上面)
//2.當使用FLAG_TRANSLUCENT_STATUS 的時候getWindowVisibleDisplayFrame拿到的是不包含系統狀態欄的高度可是又頂上了系統欄的位置,致使出錯
//綜合上述幾點正確作法爲當前代碼實現,即便用系統默認算好的mChildOfContent高度,當鍵盤喚起,保留當前mChildOfContent高度,鍵盤消失再設置回去
frameLayoutParams.height = mChildOfContentSize;
}
mChildOfContent.requestLayout();
usableHeightPrevious = frameLayoutParams.height;
}
}
/**
* 計算mChildOfContent可見高度 ** @return
*/
/* private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
*/
}
複製代碼