android 软键盘在全屏下的布局计算问题

来源:互联网 发布:冷焰火在淘宝上怎么买 编辑:程序博客网 时间:2024/05/19 06:37

转载  http://blog.csdn.net/a2758963/article/details/25280507?utm_source=tuicool

在非全屏模式下,将activity的windowSoftInputMode的属性设置为:adjustResize。同时在View的onSizeChanged(int w, int h, int oldw, int oldh)里可以得到变化后的尺寸,然后根据前后变化的结果来计算屏幕需要移动的距离。

但是在全屏模式下,即使将activity的windowSoftInputMode的属性设置为:adjustResize。在键盘显示时它未将Activity的Screen向上推动,所以你Activity的view的根树的尺寸是没有变化的。在这种情况下,你也就无法得知键盘的尺寸,对根view的作相应的推移。全屏下的键盘无法Resize的问题从2.1就已经存在了,直到现在google还未给予解决。

如果你想了解android 软键盘普通情况下显示与隐藏的问题,你可以了解这篇博客android 软键盘的显示与隐藏问题的研究,它可以帮助你解决在非全屏下,软键盘的显示时,你不能很好地控制布局的问题。

感谢Ricardo提供的轮子,他在stackoverflow找到了解决方案。有人已经封装好了该类,你只需引用就OK了,我们来看下这个帮助类。

帮助类源码:

//Workaround to get adjustResize functionality for input methos when the fullscreen mode is on//found by Ricardo//taken from http://stackoverflow.com/a/19494006import 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);    }}

可以从源码看到其中的原理。

1.利用viewTreeobserver来监听View根树的变化。一旦键盘显示或隐藏都会引起addOnGlobalLayoutListener()方法调用。

2.计算Activtiy可视区域的高度,然后与上次可视区域高度进行比较,若相等,界面出现在变化。

3.如果界面出现变化,计算可视区域高度visibleHeight与Activity根view的高度rootHeight之差heightDifference。

4.如果heightDifference > (rootHeight / 4), 则认为键盘显示,否则认为键盘隐藏。

5.根据键盘变化的状态,对Activity的contentView进行高度属性作出改变。

至此,你已经完成了对键盘显示或隐藏对contentView的Resize了。


使用方法

在你的Activity的oncreate()方法里调用AndroidBug5497Workaround.assistActivity(this);即可。注意:在setContentView(R.layout.xxx)之后调用。


0 0