1、用一個Space頂上去。
2、用ScrollView解決。
遇到的坑:
1、使用方案1時,部分手機有虛擬鍵盤,需判斷在有虛擬鍵盤上的手機加一個虛擬鍵盤的高度。
方案1:用一個Space頂上去。
xml:
<FrameLayout>
<主內容/>
<LinearLayout>
<EditText/>
</Space>
</LinearLayout>
</FrameLayout>
複製程式碼
java:
private KeyboardChangeListener mKeyboardChangeListener;
private boolean isVisiableForLast;
private void initListener(){
mKeyboardChangeListener = new KeyboardChangeListener(_mActivity);
mKeyboardChangeListener.setKeyBoardListener((isShow, keyboardHeight) -> ALog.e(TAG, "isShow = [" + isShow + "], keyboardHeight = [" + keyboardHeight + "]"));
final View decorView = _mActivity.getWindow().getDecorView();
//計算出可見螢幕的高度
//獲得螢幕整體的高度
//獲得鍵盤高度
globalLayoutListener = () -> {
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
//計算出可見螢幕的高度
int displayHight = rect.bottom - rect.top;
//獲得螢幕整體的高度
int hight = decorView.getHeight();
//獲得鍵盤高度
int keyboardHeight = hight - displayHight;
boolean visible = (double) displayHight / hight < 0.8;
if (visible != isVisiableForLast) {
listener.onSoftKeyBoardVisible(visible, keyboardHeight);
}
isVisiableForLast = visible;
};
//註冊佈局變化監聽
decorView.getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
}
IKeyBoardVisibleListener listener = new IKeyBoardVisibleListener() {
@Override
public void onSoftKeyBoardVisible(boolean visible, int windowBottom) {
windowBottom -= ScreenUtils.getStatusBarHeight(InputFromActivity.this);
if (checkDeviceHasNavigationBar(InputFromActivity.this)) {
windowBottom -= getBottomKeyboardHeight();
}
if (visible) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, windowBottom);
spaceInput.setLayoutParams(lp);
} else {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
spaceInput.setLayoutParams(lp);
}
Log.d(TAG, "onSoftKeyBoardVisible ---- visible:" + visible + " windowBottom:" + windowBottom);
}
};
interface IKeyBoardVisibleListener {
void onSoftKeyBoardVisible(boolean visible, int windowBottom);
}
//------------------------ 檢測虛擬鍵盤部分----------------------
//獲取是否存在NavigationBar
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);
}
try {
Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
Method m = systemPropertiesClass.getMethod("get", String.class);
String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
if ("1".equals(navBarOverride)) {
hasNavigationBar = false;
} else if ("0".equals(navBarOverride)) {
hasNavigationBar = true;
}
} catch (Exception e) {
}
return hasNavigationBar;
}
//獲取虛擬鍵盤高度
public int getBottomKeyboardHeight() {
int screenHeight = getAccurateScreenDpi()[1];
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int heightDifference = screenHeight - dm.heightPixels;
return heightDifference;
}
/**
* 獲取精確的螢幕大小
*/
public int[] getAccurateScreenDpi() {
int[] screenWH = new int[2];
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
try {
Class<?> c = Class.forName("android.view.Display");
Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
method.invoke(display, dm);
screenWH[0] = dm.widthPixels;
screenWH[1] = dm.heightPixels;
} catch (Exception e) {
e.printStackTrace();
}
return screenWH;
}
複製程式碼
方案2:用ScrollView解決。
xml:
<Fragment>
<ScrollView>
</主內容>
</ScrollView>
<LinearLayout>
<EditText/>
</LinearLayout>
</Fragment>
複製程式碼
注:採用第二種方案無序寫java程式碼
建議:
方案1最好用在主內容是RecyclerView或ListView的佈局,原因是防止和ScrollView發生衝突。