Android Activity 键盘弹出 布局重新分配空间

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_35192370/article/details/79362533

在 manifest 中 设置:

android:windowSoftInputMode="stateHidden|adjustResize"
或者代码中设置: getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

在setContentView()之前调用

SOFT_INPUT_ADJUST_NOTHING:         不调整(输入法完全直接覆盖住,未开放此参数)
SOFT_INPUT_ADJUST_PAN:                 把整个Layout顶上去露出获得焦点的EditText,不压缩多余空间
SOFT_INPUT_ADJUST_RESIZE:            整个Layout重新编排,重新分配多余空间
SOFT_INPUT_ADJUST_UNSPECIFIED:  系统自己根据内容自行选择上两种方式的一种执行(默认配置)

但是当状态栏设置为透明的时候 会失效:

解决办法 重写xml 的跟布局 

public class TMResizeLinearLayout extends LinearLayout {
    private int[] mInsets = new int[4];

    public TMResizeLinearLayout(Context context) {
        super(context);
    }

    public TMResizeLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TMResizeLinearLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public final int[] getInsets() {
        return mInsets;
    }

    /**
     * 此方法以过期,当应用最低API支持为20后,可以重写以下方法
     *
     * @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;
        }
    }

    @Override
    protected final boolean fitSystemWindows(Rect insets) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            mInsets[0] = insets.left;
            mInsets[1] = insets.top;
            mInsets[2] = insets.right;
            return super.fitSystemWindows(insets);
        } else {
            return super.fitSystemWindows(insets);
        }
    }

}

在xml中引用

需要在属性中 设置:

android:fitsSystemWindows="true"

否则不会生效



猜你喜欢

转载自blog.csdn.net/baidu_35192370/article/details/79362533