android - 显示对话框后或 EditText 获得焦点时,隐藏的导航栏(带有主页、后退和概览按钮的底部栏)可见

标签 android android-edittext android-dialog android-fullscreen

有一个全屏 Activity ,使用以下代码块实现相同的目的:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

// ….
}

@Override
    protected void onResume() {
        Log.i(TAG, "onResume");

        super.onResume();
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        // Set the content to appear under the system bars so that the
                        // content doesn't resize when the system bars hide and show.
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        // Hide the nav bar and status bar
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN);
    }

但是,当显示自定义对话框或编辑文本获得焦点时,底部导航栏会变得可见并固定在周围。 官方文档说:

If a new activity or dialog appears in the foreground, taking focus and partially covering the activity in progress, the covered activity loses focus and enters the Paused state. Then, the system calls onPause() on it.

When the covered activity returns to the foreground and regains focus, it calls onResume().

但是当对话框失去焦点时,onResume() 不会被调用(通过日志检查)。

还尝试在自定义对话框的 onCreate() 中添加以下代码:

View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        // Set the content to appear under the system bars so that the
                        // content doesn't resize when the system bars hide and show.
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        // Hide the nav bar and status bar
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN);

添加上述代码后,当自定义对话框在前台时,导航栏会隐藏,但当它离开焦点时,导航栏会弹回到屏幕上。

还尝试使用样式设置全屏模式。请引用以下相同。

<style name="FullScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="android:windowContentTransitions">true</item>
</style>

edittext 也是如此。 Activity 以全屏模式启动,但一旦 edittext 获得焦点,底部导航栏就可见并一直停留。

如有任何意见,我们将不胜感激。

干杯!

最佳答案

试试这段代码。

fun View.setImmersiveMode() {
    isFocusableInTouchMode = false
    setOnClickListener {
        requestFocusFromTouch()
    }
}
editText.setImmersiveMode()

关于android - 显示对话框后或 EditText 获得焦点时,隐藏的导航栏(带有主页、后退和概览按钮的底部栏)可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53794548/

相关文章:

java - 基本的java计算问题

java - 即使在引用 Activity 而不是应用程序上下文之后也出现 BadTokenException

android - 无法从 https ://settings. crashlytics.com/spi/v2/platforms/android/apps/检索设置

android - 使用 XMPP 在 Android 上推送用户通知 - 以任何方式定位这些用户,这样他们就不会去 Google Talk 等

Android:在锁屏上覆盖 TextView

android - 如何在android启动时创建PopupWindow?

android - Jetpack compose 中的 ProgressDialog

不修改集合时抛出 Java ConcurrentModificationException

android - 如何防止触摸时出现软键盘

Android:Listview 不使用键盘向上移动