Android - Hide the navigation bar
发布日期:2021-05-06 23:02:43 浏览次数:24 分类:精选文章

本文共 4731 字,大约阅读时间需要 15 分钟。

Hide the Navigation Bar in Android

Hide the Navigation Bar

To hide the navigation bar in Android 4.0 (API level 14) and later versions, you can use the SYSTEM_UI_FLAG_HIDE_NAVIGATION flag. This approach allows you to hide both the navigation bar and the status bar. However, you should also ensure that your app handles the status bar visibility appropriately, as detailed below.

Sample Code

View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

Important Notes

  • User Interaction: When the user interacts with the screen, the navigation bar (and status bar) will reappear and remain visible. The flags will be cleared upon user interaction.

  • Resuming the Activity: After the flags are cleared, you need to reset them if you want to hide the bars again. You can achieve this by listening for UI visibility changes in your onResume() method.

  • Timing is Critical: Setting the UI flags in the onCreate() method may not work correctly if the user navigates away from the activity and then returns. Instead, set the flags in onResume() or onWindowFocusChanged() to ensure they persist as the user navigates.

  • View Visibility: The setSystemUiVisibility() method only takes effect if the view you're calling it from is visible.

  • Flag Persistence: Navigating away from the view will clear the flags set using setSystemUiVisibility().

  • Make Content Appear Behind the Navigation Bar

    Starting from Android 4.1 (API level 16), you can set your app's content to appear behind the navigation bar. This ensures that the content does not resize when the navigation bar is hidden or shown. Use the SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION flag for this purpose, along with SYSTEM_UI_FLAG_LAYOUT_STABLE to maintain a stable layout.

    Example

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    View decorView = getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
    decorView.setSystemUiVisibility(uiOptions);
    }

    Key Considerations

    • Layout Stability: Use SYSTEM_UI_FLAG_LAYOUT_STABLE to ensure your app's layout remains consistent even when system bars are hidden or shown.
    • Content Visibility: Ensure that critical UI elements are positioned correctly so that they do not get covered by the system bars.

    Development Environment

    • IDE: Android Studio 4.1.1
    • Virtual Devices: Pixel 2 API 28
    • Minimum SDK: API 16 (Android 4.1)

    Project Setup

    • Effect: The app demonstrates hiding the navigation bar and showing content behind it.
    • Project Creation: Start with an Empty Activity, setting the Minimum SDK to API 16.
    • Layout File: Modify src/main/res/layout/activity_main.xml to include a Toolbar and a Button.

    Layout Configuration

    Activity Code

    package com.mk;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.appcompat.widget.Toolbar;
    import android.os.Build;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    View decorView = getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
    decorView.setSystemUiVisibility(uiOptions);
    }
    }
    });
    }
    @Override
    protected void onResume() {
    super.onResume();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    View decorView = getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
    | View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);
    }
    }
    }

    Summary

    This guide provides a comprehensive overview of hiding the navigation bar in Android applications. By using the appropriate system UI flags and understanding the nuances of UI visibility, you can create a more immersive user experience while ensuring your app behaves correctly when interacting with system bars.

    上一篇:Android - Enable fullscreen mode
    下一篇:Android - Hide the status bar

    发表评论

    最新留言

    哈哈,博客排版真的漂亮呢~
    [***.90.31.176]2025年04月19日 16时40分05秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章

    java-图形用户界面(GUI)之AWT编程-整体思路与代码架构 2025-04-01
    java-如何给表格添加分页 2025-04-01
    Java-环境搭建(Mac版) 2025-04-01
    Java-笔记12 2025-04-01
    java-设计模式-装饰器设计模式,代理设计模式和继承三种扩展方法的比较 2025-04-01
    java.lang.Object 对象中 hashCode 和 equals 方法详解 2025-04-01
    java.io.IOException: Tried to send an out-of-range integer as a 2-byte value :79944 2025-04-01
    java.io.tmpdir 2025-04-01
    java.lang.Class.isPrimitive()用法解析 2025-04-01
    java.lang.ClassNotFoundException: com.fasterxml.classmate.TypeResolver 2025-04-01
    java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener 2025-04-01
    java.lang.ClassNotFoundException后续 2025-04-01
    java.lang.IllegalArgumentException: Control character in cookie value or attribute. 2025-04-01
    java.lang.IllegalArgumentException: Invalid character found in the request target. 2025-04-01
    java.lang.IllegalStateException Failed to load ApplicationContext 解决办法 2025-04-01
    java.lang.IllegalStateException: Optional int parameter 'id' is not present but cannot be translated 2025-04-01
    java.lang.NoClassDefFoundError+ (wrong name) 2025-04-01
    java.lang.NoClassDefFoundError: javax transaction SystemException 解决方法! 2025-04-01
    java.lang.NoClassDefFoundError: javax/persistence/EntityListeners解决 2025-04-01
    java.lang.NoClassDefFoundError: kotlin/reflect/jvm/internal/KotlinReflectionInternalError 2025-04-01