这个问题google了一下,答案都是一样的。但在使用过程中,发现在有虚拟键的情况下有问题。所以自己做了进一步优化。自己测试通过的机型有红米note5plus、华为p9、魅蓝note2、荣耀V8、华为nova2 plus。
完整代码如下:
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.FrameLayout;
import java.lang.reflect.Method;
/*全屏模式下,即使将activity的windowSoftInputMode的属性设置为:adjustResize,
在键盘显示时它未将Activity的Screen向上推动,所以你Activity的view的根树的尺寸是没有变化的。
在这种情况下,你也就无法得知键盘的尺寸,对根view的作相应的推移。全屏下的键盘无法Resize的问题从2.1就已经存在了,直到现在google还未给予解决。*/
public class AndroidBug5497Workaround implements ViewTreeObserver.OnGlobalLayoutListener{
private Activity activity;
public static AndroidBug5497Workaround init (Activity activity) {
return new AndroidBug5497Workaround(activity);
}
private View mChildOfContent;
private int usableHeightPrevious=0;
private FrameLayout.LayoutParams frameLayoutParams;
/*底部导航栏的比率*/
private static final double NAVIGATION_RATIO = 0.07;
private AndroidBug5497Workaround(Activity activity) {
this.activity = activity;
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(this);
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
/**
* @describe: 销毁
* @date: 2018/3/12
*/
public void destory(){
if(mChildOfContent!=null){
mChildOfContent.getViewTreeObserver().removeOnGlobalLayoutListener(this);
activity = null;
}
}
/*重新设置高度*/
private void possiblyResizeChildOfContent(Activity activity) {
int usableHeightNow = computeUsableHeight(activity);
if (usableHeightNow != usableHeightPrevious) {
int screenRealHeight = getScreenRealHeight(activity);
/*有虚拟键的处理*/
int bottomNavigationHeight = screenRealHeight- usableHeightNow;
if(bottomNavigationHeight>0 && bottomNavigationHeight<=screenRealHeight*NAVIGATION_RATIO){
screenRealHeight = screenRealHeight-bottomNavigationHeight;
}
int heightDifference = screenRealHeight - usableHeightNow;
if (heightDifference > (screenRealHeight/4)) {//软键盘弹出的情况
frameLayoutParams.height = screenRealHeight - heightDifference;
} else {
// keyboard probably just became hidden
frameLayoutParams.height = screenRealHeight;
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
//获取屏幕原始尺寸高度,包括虚拟功能键高度
private int getScreenRealHeight(Context context){
int dpi = 0;
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
@SuppressWarnings("rawtypes")
Class c;
try {
c = Class.forName("android.view.Display");
@SuppressWarnings("unchecked")
Method method = c.getMethod("getRealMetrics",DisplayMetrics.class);
method.invoke(display, displayMetrics);
dpi=displayMetrics.heightPixels;
}catch(Exception e){
e.printStackTrace();
}
return dpi;
}
private int computeUsableHeight(Activity activity) {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top)+DensityUtil.getStatusHeight(activity);
}
@Override
public void onGlobalLayout() {
possiblyResizeChildOfContent(activity);
}
}
在Activity中使用:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirm_order);
ButterKnife.bind(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//沉浸式状态栏
immersionBar = ImmersionBar.with(this).statusBarDarkFont(true, 0.2f);
immersionBar.init();
bug5497Workaround = AndroidBug5497Workaround.init(this);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (immersionBar != null) {
immersionBar.destroy();
}
if (presenter != null) {
presenter.destory();
}
if(bug5497Workaround!=null){
bug5497Workaround.destory();
}
}
本人水平有限,有什么不对的地方,欢迎指正。