输入法遮挡问题 解决输入法遮挡的问题 基本上有两种
adjustResize + ScrollView
adjustPan
adjustPan会把页面整体上推
adjustResize则是缩放可调整页面 所以要和ScrollView配合 但是如果界面设成全屏模式就不会生效
解决方式 在非全屏模式(即状态栏不透明)下,将activity的windowSoftInputMode的属性设置为:adjustResize。同时在View的onSizeChanged(int w, int h, int oldw, int oldh)里可以得到变化后的尺寸,然后根据前后变化的结果来计算屏幕需要移动的距离。
即添加:
1 android:windowSoftInputMode="adjustResize"
但是在全屏模式下,即使将activity的windowSoftInputMode的属性设置为:adjustResize。
在键盘显示时它未将Activity的Screen向上推动,所以你Activity的view的根树的尺寸是没有变化的。
在这种情况下,你也就无法得知键盘的尺寸,对根view的作相应的推移。 全屏下的键盘无法Resize的问题从2.1就已经存在了,直到现在google还未给予解决。
有人已经封装好了该类,你只需引用就OK了,我们来看下这个类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 public class SoftHideKeyBoardUtil { public static void assistActivity (Activity activity) { new SoftHideKeyBoardUtil (activity); } private View mChildOfContent; private int usableHeightPrevious; private FrameLayout.LayoutParams frameLayoutParams; private int contentHeight; private boolean isfirst = true ; private int statusBarHeight; private SoftHideKeyBoardUtil (Activity activity) { FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content); mChildOfContent = content.getChildAt(0 ); mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver .OnGlobalLayoutListener() { public void onGlobalLayout () { if (isfirst) { contentHeight = mChildOfContent.getHeight(); isfirst = false ; } 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 )) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ frameLayoutParams.height = usableHeightSansKeyboard - heightDifference + statusBarHeight; } else { frameLayoutParams.height = usableHeightSansKeyboard - heightDifference; } } else { frameLayoutParams.height = contentHeight; } mChildOfContent.requestLayout(); usableHeightPrevious = usableHeightNow; } } private int computeUsableHeight () { Rect r = new Rect (); mChildOfContent.getWindowVisibleDisplayFrame(r); return (r.bottom - r.top); } }
使用方法 在你的Activity的onCreate()方法里调用即可
1 SoftHideKeyBoardUtil.assistActivity(this );
注意:在setContentView(R.layout.xxx)之后调用