Android:键盘上方的FAB结合SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN标志

问题描述 投票:0回答:1

问题是使用此标志时FAB不会停留在键盘上方:

this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

我尝试过:

android:fitsSystemWindows="true"

它可以工作,但是会删除SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN效果。

android:windowSoftInputMode="adjustResize"

它不起作用。

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

它也不起作用

任何建议?

android keyboard fullscreen floating-action-button flags
1个回答
0
投票

如果您使用系统UI方法(https://developer.android.com/training/system-ui/immersive.html),我刚刚找到了一个简单可靠的解决方案。

[在您使用View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN的情况下有效,例如如果您使用的是CoordinatorLayout。

[它不适用于WindowManager.LayoutParams.FLAG_FULLSCREEN(您也可以使用android:windowFullscreen在主题中设置的那个,但是您可以使用SYSTEM_UI_FLAG_LAYOUT_STABLE实现类似的效果(根据文档,它具有“相同的视觉效果”))并且此解决方案应再次起作用。

getWindow()。getDecorView()。setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION / *如果要隐藏导航* /|查看。SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LAYOUT_STABLE)

我已经在运行棉花糖的设备上对其进行了测试。

关键是软键盘也是系统窗口之一(例如状态栏和导航栏),因此系统调度的WindowInsets包含有关它的准确和可靠的信息。

对于例如在DrawerLayout中试图在状态栏后面绘制的用例,我们可以创建仅忽略顶部插图的布局,并应用底部插图作为软键盘。

这是我的自定义FrameLayout:

/**
 * Implements an effect similar to {@code android:fitsSystemWindows="true"} on Lollipop or higher,
 * except ignoring the top system window inset. {@code android:fitsSystemWindows="true"} does not
 * and should not be set on this layout.
 */
public class FitsSystemWindowsExceptTopFrameLayout extends FrameLayout {

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

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

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

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    public FitsSystemWindowsExceptTopFrameLayout(Context context, AttributeSet attrs,
                                                 int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public WindowInsets onApplyWindowInsets(WindowInsets insets) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setPadding(insets.getSystemWindowInsetLeft(), 0, insets.getSystemWindowInsetRight(),
                    insets.getSystemWindowInsetBottom());
            return insets.replaceSystemWindowInsets(0, insets.getSystemWindowInsetTop(), 0, 0);
        } else {
            return super.onApplyWindowInsets(insets);
        }
    }
}

并使用它:

<com.example.yourapplication.FitsSystemWindowsExceptTopFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Your original layout here -->
</com.example.yourapplication.FitsSystemWindowsExceptTopFrameLayout>

功劳归于:@Hai Zhang

© www.soinside.com 2019 - 2024. All rights reserved.