android 软键盘在全屏下设置adjustResize无效的问题

来源:互联网 发布:亨德尔 水上音乐 知乎 编辑:程序博客网 时间:2024/05/16 06:27

全屏模式下,即使将activity的windowSoftInputMode的属性设置为:adjustResize,在键盘显示时它未将Activity的Screen向上推动,所以你Activity的view的根树的尺寸是没有变化的。在这种情况下,你也就无法得知键盘的尺寸,对根view的作相应的推移。全屏下的键盘无法Resize的问题从2.1就已经存在了,直到现在google还未给予解决。
感谢Ricardo提供的轮子,他在stackoverflow找到了解决方案。有人已经封装好了该类,你只需引用就OK了
参考:http://stackoverflow.com/questions/7417123/android-how-to-adjust-layout-in-full-screen-mode-when-softkeyboard-is-visible/19494006#19494006

解决问题:例如聊天输入条(android:layout_alignParentBottom=”true”) 当软键盘弹出的时候 不浮起来
用 法: 在setContentView(R.layout.xxx);之后调用AndroidBug5497Workaround.assistActivity(this);

AndroidBug5497Workaround.java:

> import android.app.Activity; import android.graphics.Rect; import> android.view.View; import android.view.ViewTreeObserver; import> android.widget.FrameLayout;> >   public class AndroidBug5497Workaround {>     // For more information, see https://code.google.com/p/android/issues/detail?id=5497>     // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.> >     public static void assistActivity (Activity activity) {>         new AndroidBug5497Workaround(activity);>     }> >     private View mChildOfContent;>     private int usableHeightPrevious;>     private FrameLayout.LayoutParams frameLayoutParams;> >     private AndroidBug5497Workaround(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;>             if (heightDifference > (usableHeightSansKeyboard/4)) {>                 // keyboard probably just became visible>                 frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;>             } else {>                 // keyboard probably just became hidden>                 frameLayoutParams.height = usableHeightSansKeyboard;>             }>             mChildOfContent.requestLayout();>             usableHeightPrevious = usableHeightNow;>         }>     }> >     private int computeUsableHeight() {>         Rect r = new Rect();>         mChildOfContent.getWindowVisibleDisplayFrame(r);>         return (r.bottom - r.top);>     }> > }
0 0