新建一个类
public class WindowSoftModeAdjustResizeExecutor {
public static void assistActivity(Activity activity) {
new WindowSoftModeAdjustResizeExecutor(activity);
}
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private WindowSoftModeAdjustResizeExecutor(Activity activity) {
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return (r.bottom - r.top);
}
return r.bottom;
}
}
在使用的时候。哪个Activity被遮挡了,就调用这句代码
/**
* 解决透明状态栏下,弹出键盘遮挡输入框的问题
*/
WindowSoftModeAdjustResizeExecutor.assistActivity(this);
并且要在清单文件对应的activity里面写
android:windowSoftInputMode="adjustResize|stateHidden"