
本文共 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 barint 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 aToolbar
and aButton
.
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.
发表评论
最新留言
关于作者
