Android中adjustResize失效的解决办法之一

在沉浸式状态栏下键盘属性设置adjustResize会失效,这时候只需要在需要弹起布局的根布局中添加android:fitsSystemWindows="true"即可。
但是在这种情况下,你的titlebar会下移statusbar的高度的距离。所以就必须重写一个layout继承自你所使用的layout,并重写其中的两个方法,贴出代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 public class MyLinearLayout extends LinearLayout{

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

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

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

public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MyLinearLayout(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.
// TODO: Figure out why.

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();
Log.e("mInsets[0]",""+mInsets[0]);
mInsets[1] = insets.getSystemWindowInsetTop();
Log.e("mInsets[1]",""+mInsets[1]);
mInsets[2] = insets.getSystemWindowInsetRight();
Log.e("mInsets[2]",""+mInsets[2]);
return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0,
insets.getSystemWindowInsetBottom()));
} else {
return insets;
}
}
}