参考网址:https://blog.csdn.net/qq_43278826/article/details/90211583
https://www.jianshu.com/p/306482e17080 ----代码详解
1.点击Editext弹出输入框,EditText不被覆盖,并且整体布局不上移
<activity
android:name=".activity.MainActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize|stateHidden"/>
<!--
解决点击Editext弹出输入框,整体界面上移问题;加上下边这句后布局不会整体上移,只有EditText会
向上移动,不被输入框遮挡
android:windowSoftInputMode="adjustResize|stateHidden" -->
2.点击WebView中EditText布局上移
package com.sswl.sdk.utils;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
/**
* 用于修复Android 5497BUG,即adjustResize与全屏不能共存
* <p>
* 使用方法: setContentView(); 后调用 AndroidBug5497Workaround.assistActivity(this);
*
* @author Administrator
*/
public class AndroidBug5497Workaround {
/**
* 适用于调整activity内的布局大小
*/
public static void assistActivity(Activity activity) {
new AndroidBug5497Workaround(activity);
}
/**
* 适用于调整popuwindow/dialog布局大小
*
* @param activity
* @param contentView
*/
public static void assistActivity(Activity activity, ViewGroup contentView) {
new AndroidBug5497Workaround(activity, contentView);
}
private Activity activity;
private View mChildOfContent;
private int usableHeightPrevious;
private ViewGroup.LayoutParams frameLayoutParams;
private boolean isRootContentView = false;
//适用于调整activity内的布局大小
private AndroidBug5497Workaround(Activity activity) {
this.activity = activity;
isRootContentView = false;
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
//获取当前界面的根布局
mChildOfContent = content.getChildAt(0);
if (mChildOfContent == null) {
mChildOfContent = (FrameLayout) activity.findViewById(android.R.id.content);
}
//在根布局增加布局变化的监听
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = (ViewGroup.LayoutParams) mChildOfContent.getLayoutParams();
}
//适用于调整popuwindow/dialog布局大小
private AndroidBug5497Workaround(Activity activity, ViewGroup contentView) {
this.activity = activity;
isRootContentView = true;
//获取当前界面的根布局
mChildOfContent = contentView;
//在根布局增加布局变化的监听
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = (ViewGroup.LayoutParams) mChildOfContent.getLayoutParams();
}
private void possiblyResizeChildOfContent() {
//可见的高度
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
//根布局的高度
int rootViewHeight = mChildOfContent.getRootView().getHeight();
//这个判断是为了解决19之前的版本不支持沉浸式状态栏导致布局显示不完全的问题
if (VERSION.SDK_INT < VERSION_CODES.KITKAT) {
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
rootViewHeight -= statusBarHeight;
}
int heightDifference = Math.abs(rootViewHeight - usableHeightNow);
//当前可见区域大小不足根布局大小的5/6时,认为是软键盘弹出
if (heightDifference > (rootViewHeight /6)) {
// Log.e("min77","computeUsableHeight heightDifference > rootViewHeight / 6");
// keyboard probably just became visible
frameLayoutParams.height = usableHeightNow;
} else {
// keyboard probably just became hidden
frameLayoutParams.height = rootViewHeight;
}
//更新布局大小
mChildOfContent.setLayoutParams(frameLayoutParams);
mChildOfContent.requestLayout();
Log.i("min77", "computeUsableHeight 刷新 rootViewHeight = "+rootViewHeight+" ,frameLayoutParams.height = " + frameLayoutParams.height + " , usableHeightPrevious = " + usableHeightPrevious);
usableHeightPrevious = usableHeightNow;
}
}
/**
* 计算当前界面可见的高度
*/
private int computeUsableHeight() {
Rect frame = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(frame);
Log.i("min77", "computeUsableHeight = " + (frame.bottom - frame.top));
return (frame.bottom - frame.top);
// 全屏模式下: return frame.bottom;
// 非全屏模式下: return (frame.bottom - frame.top);
}
}
前提:
AndroidManifest.xml的activity设置了 android:windowSoftInputMode="adjustResize",需要去掉android:windowSoftInputMode才生效
使用:
1.在嵌有webview的activity的setContentView之前
AndroidBug5497Workaround.assistActivity(this);
2.在popuwindow show之后
//这个必须设置,否则无法将布局顶上去
AndroidBug5497Workaround.assistActivity(mActivityRef.get(),mContentView);
3.布局中WebView和EditText同级
说明:当布局的同一级中有WebView和EditText时,使用上边1和2的方法无法实现Editext输入框向上移动;此时如果EditText位于布局最底部,点击输入框会弹出软键盘但是会遮挡住底部的EditText。
//设置弹出软键盘时布局整体向上移,这样不会遮盖Editext
//在Activity的onCreate方法中,调用setContentView之前添加以下代码
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
<activity
android:name=".activity.MainActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan"/>
使用以上两种方法都可以实现WebView同级下的EditText不被软键盘遮挡