一般用法
- 设置属性:
android:windowSoftInputMode="stateVisible|adjustResize"
android:fitsSystemWindows="true"
以上做法会导致toolbar向下平移了statusbar的高度,也就是说statusbar是全白的。。
解决办法:
- 自定义CustomInsetsFrameLayout
public class CustomInsetsFrameLayout extends FrameLayout{
private int[] mInsets = new int[4];
public CustomInsetsFrameLayout(@NonNull Context context) {
super(context);
}
public CustomInsetsFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public CustomInsetsFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public final int[] getInsets() {
return mInsets;
}
@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);
}
}
- 设置属性
android:windowSoftInputMode="stateVisible|adjustResize"
android:fitsSystemWindows="true"
完美适配!