You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android API 33下如何禁用滑动唤起的系统导航栏?

Android API 33 禁用底部滑动唤起系统导航栏解决方案

针对你自定义导航按钮后,滑动仍唤起系统导航栏的问题,可通过以下方案解决:

方案1:使用WindowInsetsController(推荐,API30+)

Android 11及以上提供WindowInsetsController用于精细控制系统栏行为,可有效阻止滑动唤起导航栏:

Kotlin 代码示例

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // 让布局延伸至系统栏区域,避免内容被挤压
    window.setDecorFitsSystemWindows(false)

    window.insetsController?.apply {
        // 隐藏系统导航栏
        hide(WindowInsets.Type.navigationBars())
        // 设置系统栏行为:仅允许点击唤起,禁用滑动唤起
        systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_BARS_BY_TAP
    }
}

Java 代码示例

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getWindow().setDecorFitsSystemWindows(false);

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

若需要完全禁止任何方式唤起导航栏,可额外添加拦截逻辑:

window.decorView.setOnApplyWindowInsetsListener { view, insets ->
    view.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0, 0))
}

方案2:结合旧版沉浸式标志(兼容场景)

如果方案1不生效,可尝试旧版系统UI标志(API33仍兼容):

window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
        or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        or View.SYSTEM_UI_FLAG_FULLSCREEN
        or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)

注:SYSTEM_UI_FLAG_IMMERSIVE会让导航栏仅在主动点击时显示,滑动操作不会触发唤起。

针对DrawerLayout的额外处理

若你使用了DrawerLayout,需同时禁用其边缘滑动触发逻辑,避免和系统导航栏滑动冲突:

Kotlin 代码示例

val drawerLayout = findViewById<DrawerLayout>(R.id.drawer_layout)
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
// 禁用边缘滑动触发抽屉的区域
drawerLayout.setDrawerEdgeSize(0)

XML 布局设置

<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawerEdgeSize="0dp"
    android:lockMode="lockedClosed">

    <!-- 布局内容 -->
</androidx.drawerlayout.widget.DrawerLayout>

内容的提问来源于stack exchange,提问作者Roman Kozulia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 13:20:27