Android

沉浸式+虚拟键盘遮挡输入框+底部导航条被虚拟按键遮挡

2018-02-06  本文已影响90人  小YiY

今天遇到了个问题...
之前设置APP为沉浸式,同时解决了软键盘弹出遮挡输入框的问题。
先说之前的解决方式:
加入AndroidBug5497Workaround操作类,这种方式网上很多人都提到。

一开始使用并没有问题。
有一天拿一个带虚拟按键的手机测试,发现了底部的导航栏完全被虚拟按键遮挡了。

先说解决方式:

修改AndroidBug5497Workaround类:代码中无法加粗,修改的两个地方用【此处】标记了。

import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

/**
 * Created by bhj on 2018/1/30.
 */

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 int contentHeight;//获取setContentView本来view的高度
    private boolean isfirst = true;//只用获取一次

    private AndroidBug5497Workaround(final Activity activity) {
        final Activity activity1 = 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(activity1);
            }
        });
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }


    // 获取界面可用高度,如果软键盘弹起后,Activity的xml布局可用高度需要减去键盘高度
    private void possiblyResizeChildOfContent(Activity activity) {
        //1、获取当前界面可用高度,键盘弹起后,当前界面可用布局会减少键盘的高度
        int usableHeightNow = computeUsableHeight(activity);
        if (usableHeightNow != usableHeightPrevious) {
            //3、获取Activity中xml中布局在当前界面显示的高度
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            //4、Activity中xml布局的高度-当前可用高度
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            //5、高度差大于屏幕1/4时,说明键盘弹出
            if (heightDifference > (usableHeightSansKeyboard/4)) {
                // 6、键盘弹出了,Activity的xml布局高度应当减去键盘高度
                // keyboard probably just became visible
                    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard probably just became hidden
                frameLayoutParams.height = contentHeight;//【此处】当键盘隐藏时设置的高度是之前获取的contentHeight
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    private int computeUsableHeight(Activity activity) {
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        //这个判断是为了解决19之后的版本在弹出软键盘时,键盘和推上去的布局(adjustResize)之间有黑色区域的问题
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            return (r.bottom - r.top)+statusBarHeight;
        }
        return (r.bottom - r.top);
    }

}

解释下原因:

之前的操作方式(链接里的代码)是在隐藏键盘时,设置的高度为 usableHeightSansKeyboard ,
也就是mChildOfContent.getRootView().getHeight(),
获取的是rootView的高度,包含了底部的虚拟按键 高度。
所以设置我们的布局高度大了,
所以会被遮住。

设置为contentHeight,即当前view的getHeight()获取的高度,
不包括底部虚拟按键高度,
所以不被遮挡。

上一篇下一篇

猜你喜欢

热点阅读