1、新建类AndroidBug5497Workaround:

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与全屏不能共存 * 

* 使用方法: 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); }}

2、在嵌有webview的activity的setContentView之前加以下代码即可解决:

AndroidBug5497Workaround.assistActivity(this);

或者在popuwindow show之后加入以下代码:

 //这个必须设置,否则无法将布局顶上去AndroidBug5497Workaround.assistActivity(mActivityRef.get(),mContentView);

【高能提醒】

假如上述代码显示软键盘弹出时候,没有看到布局大小有变化,即没有调用到possiblyResizeChildOfContent();,那么有可能是你AndroidManifest.xml的activity设置了 android:windowSoftInputMode="adjustResize",需要去掉android:windowSoftInputMode才生效

更多相关文章

  1. 【Android】使RecyclerView 支持setEmptyView
  2. android 设置APN
  3. Android中如何获取应用版本号
  4. Android控件属性android:fitsSystemWindows="true"的坑
  5. Android(安卓)之 SeekBar用法介绍
  6. Android获取sdcard信息
  7. android 中获取屏幕大小
  8. android 如何获取当前运行的activity
  9. android之获得当前连接wifi的名字

随机推荐

  1. 根据电话号码查找人名
  2. textAppearance 解答,android系统主题样式
  3. Android(安卓)SeekBar
  4. Android(安卓)ndk-stack tool
  5. Android(安卓)gson解析json数据工具类
  6. Android设计中的.9.png
  7. Android应用程序资源管理器
  8. Android(安卓)SDK下载
  9. Android调试笔记——Installation error
  10. android进度对话框的使用