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发生冲突。
网友评论