android软键盘与沉浸式的冲突

作者: tmyzh | 来源:发表于2018-06-08 16:40 被阅读146次

    实例分析下windowSoftInputMode 里面adjustPan和adjustResize的作用,以及在沉浸式样式下的解决方案
    windowSoftInputMode

    页面布局

    在没有设置scrollview的布局下

    "adjustResize"
    该 Activity主窗口总是被调整屏幕的大小以便留出软键盘的空间。


    adjustResize 界面布局不会动,键盘会挡住输入框

    "adjustPan"
    该 Activity主窗口并不调整屏幕的大小以便留出软键盘的空间。相反,当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分。这个通常是不期望比调整大小,因为用户可能关闭软键盘以便获得与被覆盖内容的交互操作。

    adjustPan输入框获得焦点的时候会被键盘上移,不遮挡
    在设置scrollview的布局下
    adjustPan 键盘不会遮挡输入框,在原来布局不超过一屏的情况下不能滑动,超过之后只能滑动少许,这个原因有待分析。
    adjustResize可以通过滑动scrollview布局移出输入框
    总结:
    adjustPan类似于键盘底部增加一块键盘布局
    adjustResize类似于键盘覆盖在界面上面

    使用沉浸式样式时
    adjustPan 界面上移软键盘不遮挡输入框,但是上面的布局会遮挡状态栏

    663178288014871941.jpg
    adjustResize键盘会遮挡输入框,且不管是否用scrollview布局都不能滑动

    解决方案有两种

    第一种是谷歌提的AndroidBug5497Workaround,适配小米,华为,三星时需要修改代码,原本的方法在这些手机上会出现高度计算不准确的情况,修改后代码如下

    public class AndroidBug5497Workaround {
        public static void assistActivity(Activity activity) {
            new AndroidBug5497Workaround(activity);
        }
        private View mChildOfContent;
        private int usableHeightPrevious;
        private FrameLayout.LayoutParams frameLayoutParams;
        private int contentHeight;
        private   boolean isfirst = true;
        private Activity activity;
        private  int statusBarHeight;
    
        private AndroidBug5497Workaround(Activity activity) {
            //获取状态栏的高度
            int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
            statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
            this.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 usableHeightSansKeyboard2 = mChildOfContent.getHeight();//兼容华为等机型
                int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
                int heightDifference = usableHeightSansKeyboard - usableHeightNow;
                if (heightDifference > (usableHeightSansKeyboard / 4)) {
                    // keyboard probably just became visible
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                        //frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
                        frameLayoutParams.height = usableHeightSansKeyboard - heightDifference + statusBarHeight;
                    } else {
                        frameLayoutParams.height = usableHeightSansKeyboard -heightDifference;
                    }
                } else {
                    frameLayoutParams.height = contentHeight;
                }
    
                mChildOfContent.requestLayout();
                usableHeightPrevious = usableHeightNow;
            }
        }
        /**     * 计算mChildOfContent可见高度     ** @return     */
        private int computeUsableHeight() {
            Rect r = new Rect();
            mChildOfContent.getWindowVisibleDisplayFrame(r);
            return (r.bottom - r.top);
        }
    }
    

    第二种 解决方案 自定义LinearLayout,relativityLayout

    ResizeRelativityLayout

    public class ResizeLinearLayout extends LinearLayout {
    
        private int[] mInsets = new int[4];
    
        public ResizeLinearLayout(Context context) {
            super(context);
        }
    
        public ResizeLinearLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ResizeLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public ResizeLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }
    
        @Override
        protected final boolean fitSystemWindows(Rect insets) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                // Intentionally do not modify the bottom inset. For some reason,
                // if the bottom inset is modified, window resizing stops working.
    
                mInsets[0] = insets.left;
                mInsets[1] = insets.top;
                mInsets[2] = insets.right;
    
                insets.left = 0;
                insets.top = 0;
                insets.right = 0;
            }
    
            return super.fitSystemWindows(insets);
        }
    
        @Override
        public final WindowInsets onApplyWindowInsets(WindowInsets insets) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
                mInsets[0] = insets.getSystemWindowInsetLeft();
                mInsets[1] = insets.getSystemWindowInsetTop();
                mInsets[2] = insets.getSystemWindowInsetRight();
                return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0,
                        insets.getSystemWindowInsetBottom()));
            } else {
                return insets;
            }
        }
    
    }
    

    ResizeLinearLayout

    public class ResizeLinearLayout extends LinearLayout {
    
        private int[] mInsets = new int[4];
    
        public ResizeLinearLayout(Context context) {
            super(context);
        }
    
        public ResizeLinearLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ResizeLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public ResizeLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }
    
        @Override
        protected final boolean fitSystemWindows(Rect insets) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                // Intentionally do not modify the bottom inset. For some reason,
                // if the bottom inset is modified, window resizing stops working.
    
                mInsets[0] = insets.left;
                mInsets[1] = insets.top;
                mInsets[2] = insets.right;
    
                insets.left = 0;
                insets.top = 0;
                insets.right = 0;
            }
    
            return super.fitSystemWindows(insets);
        }
    
        @Override
        public final WindowInsets onApplyWindowInsets(WindowInsets insets) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
                mInsets[0] = insets.getSystemWindowInsetLeft();
                mInsets[1] = insets.getSystemWindowInsetTop();
                mInsets[2] = insets.getSystemWindowInsetRight();
                return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0,
                        insets.getSystemWindowInsetBottom()));
            } else {
                return insets;
            }
        }
    
    }
    

    使用方法:在设置了布局根部使用此控件并且加上android:fitsSystemWindows="true"

    相关文章

      网友评论

        本文标题:android软键盘与沉浸式的冲突

        本文链接:https://www.haomeiwen.com/subject/lsyosftx.html