今天遇到了個(gè)問題...
之前設(shè)置APP為沉浸式躬厌,同時(shí)解決了軟鍵盤彈出遮擋輸入框的問題。
先說之前的解決方式:
加入AndroidBug5497Workaround操作類,這種方式網(wǎng)上很多人都提到。
一開始使用并沒有問題焕参。
有一天拿一個(gè)帶虛擬按鍵的手機(jī)測(cè)試,發(fā)現(xiàn)了底部的導(dǎo)航欄完全被虛擬按鍵遮擋了。
先說解決方式:
修改AndroidBug5497Workaround類:代碼中無(wú)法加粗甘磨,修改的兩個(gè)地方用【此處】標(biāo)記了。
import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
/**
* Created by bhj on 2018/1/30.
*/
public class AndroidBug5497Workaround {
// For more information, see https://code.google.com/p/android/issues/detail?id=5497
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
public static void assistActivity (Activity activity) {
new AndroidBug5497Workaround(activity);
}
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
//為適應(yīng)華為小米等手機(jī)鍵盤上方出現(xiàn)黑條或不適配
private int contentHeight;//獲取setContentView本來(lái)view的高度
private boolean isfirst = true;//只用獲取一次
private AndroidBug5497Workaround(final Activity activity) {
final Activity activity1 = activity;
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
if (isfirst) {
contentHeight = mChildOfContent.getHeight();//【此處】獲取布局的高度(不包括底部虛擬按鍵的高度)
isfirst = false;
}
possiblyResizeChildOfContent(activity1);
}
});
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
// 獲取界面可用高度眯停,如果軟鍵盤彈起后济舆,Activity的xml布局可用高度需要減去鍵盤高度
private void possiblyResizeChildOfContent(Activity activity) {
//1?獲取當(dāng)前界面可用高度,鍵盤彈起后莺债,當(dāng)前界面可用布局會(huì)減少鍵盤的高度
int usableHeightNow = computeUsableHeight(activity);
if (usableHeightNow != usableHeightPrevious) {
//3?獲取Activity中xml中布局在當(dāng)前界面顯示的高度
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
//4?Activity中xml布局的高度-當(dāng)前可用高度
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
//5?高度差大于屏幕1/4時(shí)滋觉,說明鍵盤彈出
if (heightDifference > (usableHeightSansKeyboard/4)) {
// 6?鍵盤彈出了,Activity的xml布局高度應(yīng)當(dāng)減去鍵盤高度
// keyboard probably just became visible
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
} else {
// keyboard probably just became hidden
frameLayoutParams.height = contentHeight;//【此處】當(dāng)鍵盤隱藏時(shí)設(shè)置的高度是之前獲取的contentHeight
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight(Activity activity) {
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
//這個(gè)判斷是為了解決19之后的版本在彈出軟鍵盤時(shí)齐邦,鍵盤和推上去的布局(adjustResize)之間有黑色區(qū)域的問題
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
return (r.bottom - r.top)+statusBarHeight;
}
return (r.bottom - r.top);
}
}
解釋下原因:
之前的操作方式(鏈接里的代碼)是在隱藏鍵盤時(shí)椎侠,設(shè)置的高度為 usableHeightSansKeyboard ,
也就是mChildOfContent.getRootView().getHeight(),
獲取的是rootView的高度措拇,包含了底部的虛擬按鍵 高度我纪。
所以設(shè)置我們的布局高度大了,
所以會(huì)被遮住。
設(shè)置為contentHeight宣羊,即當(dāng)前view的getHeight()獲取的高度璧诵,
不包括底部虛擬按鍵高度,
所以不被遮擋仇冯。