Android軟鍵盤(pán)在全屏下設(shè)置adjustResize無(wú)效的問(wèn)題酪劫。
這時(shí)我們可以在activity的根布局上添加fitsSystemWindows="true"贞盯,然后adjustResize屬性就會(huì)起作用。但是這樣做還有一個(gè)問(wèn)題怎憋,我們的界面會(huì)下移一段距離淌哟,這段距離的高度相當(dāng)于狀態(tài)欄的高度。
public class WindowInsetsLinearLayout extends LinearLayout {
private static final String TAG = WindowInsetsLinearLayout.class.getSimpleName();
public WindowInsetsLinearLayout(@NonNull Context context) {
this(context, null);
}
public WindowInsetsLinearLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public WindowInsetsLinearLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private int[] mInsets = new int[4];
@Override
protected final boolean fitSystemWindows(Rect insets) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
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;
}
}
}
使用
<WindowInsetsLinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">
<TitleView
android:id="@+id/title_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/contentPanel"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</WindowInsetsLinearLayout>