界面底部的edittext被弹出的软键盘覆盖的问题解决方法

来源:互联网 发布:华为手机全球销量知乎 编辑:程序博客网 时间:2024/04/29 21:53

1:如果布局文件不存在toolbar,则直接在布局文件中根布局外面添加<Scrollview/>;

      如果布局文件中存在<include/>导入的toolbar,则可将toobar下面的所有布局包裹在<Scrollview/>中;

2:在根布局中设置参数android:fitsSystemWindows="true" ;注:如果有设置windowSoftInputMode="adjustPan",要取消,否则会冲突;

3:如果出现状态栏变透明,则在项目内重写一个CustomLayout类继承你的根布局LinearLayout或RelativeLayout等,后将你的布局文件内根布局改为CustomLayout,注意,此时需要标注清楚类路径,如com.***.***.CustomLayout。重写类的方法如下:

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.view.WindowInsets;
import android.widget.RelativeLayout;

public class SoftInputRelativeLayout extends RelativeLayout {


    private int[] mInsets = new int[4];


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


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


    @Override
    public WindowInsets computeSystemWindowInsets(WindowInsets in, Rect outLocalInsets) {


        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.
            // TODO: Figure out why.


            mInsets[0] = outLocalInsets.left;
            mInsets[1] = outLocalInsets.top;
            mInsets[2] = outLocalInsets.right;


            outLocalInsets.left = 0;
            outLocalInsets.top = 0;
            outLocalInsets.right = 0;
        }


        return super.computeSystemWindowInsets(in, outLocalInsets);


    }


    @Override
    public 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;
        }
    }

}

上述方法中,可针对你的根布局,将RelativeLayout修改成相应的需要继承的布局文件即可。

注:文章参考链接:https://segmentfault.com/q/1010000004579431

https://stackoverflow.com/questions/21092888/windowsoftinputmode-adjustresize-not-working-with-translucent-action-navbar

阅读全文
0 0