Android 全屏/沉侵式模式下/动态添加EditText/ViewStub等延迟加载,输入框被键盘遮挡问题记录

场景描述

因为在xml中加载布局时,使用了ViewStub.需要加载的布局中有EditText.导致 延迟加载

方法尝试

  1. AndroidManifest.xml 里配置android:windowSoftInputMode=“adjustResize|stateHidden”
  2. onCreate方法中setContentView之前添加getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
  3. 在根布局文件中加入:<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </LinearLayout> </ScrollView>

尝试后均均无效。应该是我动态添加的布局,导致检测不到键盘改变了布局显示的大小。思路是监测键盘弹出,再计算显示的大小,是否需要顶起布局。

借鉴了:https://blog.csdn.net/u010231682/article/details/93479895

解决方式:引入代码监听软键盘,同时在根布局添加ScrollView


import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;


public class LayoutAutoResizeHelper {
    private int usableHeightPrevious;

    private LayoutAutoResizeHelper() {

    }

    public static LayoutAutoResizeHelper getInstance() {
        return InstanceHolder.INSTANCE;
    }

    private static class InstanceHolder {
        private static final LayoutAutoResizeHelper INSTANCE = new LayoutAutoResizeHelper();
    }

    public void adjustResize(Activity activity) {
        setupAdjustResize(activity);
    }

    private void setupAdjustResize(Activity activity) {
        ViewGroup content = activity.findViewById(android.R.id.content);
        View contentView = content.getChildAt(0);
        contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent(contentView);
            }
        });
    }
    private void possiblyResizeChildOfContent(View contentView) {
        ViewGroup.LayoutParams contentViewParams = contentView.getLayoutParams();
        //获取当前界面可视区域(注:可视区域指APP界面)
        Rect rect = computeUsableRect(contentView);
        //获取当前界面可视区域高度
        int currentUsableHeight = rect.bottom - rect.top;
        if (currentUsableHeight != usableHeightPrevious) {
            //获取根布局高度
            int rootViewHeight = contentView.getRootView().getHeight();
            //计算出根布局高度与可视区域高度差值
            int heightDiff = rootViewHeight - currentUsableHeight;
            //差值超过四分之一说明软键盘弹出
            if (heightDiff > (rootViewHeight / 4)) {
                //全屏模式需要加上状态栏高度,否则低于软键盘高度的输入框弹起时与软键盘顶部会有偏差
                contentViewParams.height = rootViewHeight - heightDiff + getStatusHeight(contentView.getContext());
            } else {
                //此处需要使用rect.bottom ,因为部分手机有虚拟导航且处于开启状态时会导致底部界面被虚拟导航遮挡
                contentViewParams.height = rect.bottom;
            }
            contentView.requestLayout();
            usableHeightPrevious = currentUsableHeight;
        }
    }

    private Rect computeUsableRect(View view) {
        Rect r = new Rect();
        view.getWindowVisibleDisplayFrame(r);
        return r;
    }

    private int getStatusHeight(Context context) {
        return StatusBarUtil.getStatusBarHeight(context);//获取状态栏高度,根据项目修改补充,这里就不贴出来了
    }
}

猜你喜欢

转载自blog.csdn.net/fepengwang/article/details/104940816