如何在全屏模式下检测系统导航栏何时出现?

发布于 2025-01-21 06:51:05 字数 1928 浏览 0 评论 0原文

以下代码成功地将系统的导航栏从屏幕上隐藏起来。用户仍然可以滑动以揭示导航栏,该导航栏将在屏幕上保留几秒钟,然后再次消失。

是否有回调可以检测到导航栏何时出现并消失,因为用户向上刷卡,然后导航栏何时自动隐藏?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {

    getWindow().setDecorFitsSystemWindows(false);

    if (getWindow().getInsetsController() != null) {
        getWindow().getInsetsController().hide(WindowInsets.Type.navigationBars());
        getWindow().getInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
    }

} else {

    getWindow().getDecorView().setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
        View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
    );
}

下面的代码(取自 docs )似乎检测导航栏是在启动时隐藏的,但是当用户刷新以揭示它或之后消失时,导航栏不会。

View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
        (new View.OnSystemUiVisibilityChangeListener() {
    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        // Note that system bars will only be "visible" if none of the
        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
            // TODO: The system bars are visible. Make any desired
            // adjustments to your UI, such as showing the action bar or
            // other navigational controls.
        } else {
            // TODO: The system bars are NOT visible. Make any desired
            // adjustments to your UI, such as hiding the action bar or
            // other navigational controls.
        }
    }
});

The following code successfully hides the system's navigation bar from the screen. Users can still swipe up to reveal the navigation bar, which will remain on screen for a few seconds, then disappear again.

Is there a callback to detect when the navigation bar appears and disappears, as the user swipes up, and afterwards, when the navigation bar automatically hides?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {

    getWindow().setDecorFitsSystemWindows(false);

    if (getWindow().getInsetsController() != null) {
        getWindow().getInsetsController().hide(WindowInsets.Type.navigationBars());
        getWindow().getInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
    }

} else {

    getWindow().getDecorView().setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
        View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
    );
}

The code below (taken from the docs) seems to detect when the navigation bar is hidden on launch, but not when the user swipes up to reveal it, or when it disappears afterwards.

View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
        (new View.OnSystemUiVisibilityChangeListener() {
    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        // Note that system bars will only be "visible" if none of the
        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
            // TODO: The system bars are visible. Make any desired
            // adjustments to your UI, such as showing the action bar or
            // other navigational controls.
        } else {
            // TODO: The system bars are NOT visible. Make any desired
            // adjustments to your UI, such as hiding the action bar or
            // other navigational controls.
        }
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生路 2025-01-28 06:51:05

是否有回调可以检测到导航栏何时出现并在用户刷新时消失,之后,导航栏自动隐藏?

您无法检测到这一点,用户的滑动以显示导航栏或状态栏不是您应用程序的一部分,而是系统责任在与应用程序的过程不同的过程中运行。

实际上,您的应用程序的窗口不会在该滑动上受到影响;因此,条形图放在您的活动之上(没有任何额外的插图),这证明它与系统窗口有关。因此,应用程序/窗口中的任何听众都无济于事。

检查 this 回答更多详细信息。

Is there a callback to detect when the navigation bar appears and disappears, as the user swipes up, and afterwards, when the navigation bar automatically hides?

You can't detect that, a user's swipe to show the navigation bar or the status bar isn't a part of your app, it's the system responsibility that runs in a different process than your app's.

Actually your app's window isn't affected on that swipe; and hence the bars are laid on top of your activity (without any extra insets) and that is an evidence that it's related to the system window instead. So, any listener within your app's process/window can't help on that.

Check this answer for more detail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文