[工作积累] Android: Hide Navigation bar 隐藏导航条

https://developer.android.com/training/system-ui/navigation.html

1 View decorView = getWindow().getDecorView();
2 // Hide both the navigation bar and the status bar.
3 // SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
4 // a general rule, you should design your app to hide the status bar whenever you
5 // hide the navigation bar.
6 int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
7               | View.SYSTEM_UI_FLAG_FULLSCREEN;
8 decorView.setSystemUiVisibility(uiOptions);

Where you set the UI flags makes a difference. If you hide the system bars in your activity‘s onCreate() method and the user presses Home, the system bars will reappear. When the user reopens the activity, onCreate() won‘t get called, so the system bars will remain visible. If you want system UI changes to persist as the user navigates in and out of your activity, set UI flags in onResume() or onWindowFocusChanged().

https://developer.android.com/training/system-ui/immersive.html

 1 // This snippet hides the system bars.
 2 private void hideSystemUI() {
 3     // Set the IMMERSIVE flag.
 4     // Set the content to appear under the system bars so that the content
 5     // doesn‘t resize when the system bars hide and show.
 6     mDecorView.setSystemUiVisibility(
 7             View.SYSTEM_UI_FLAG_LAYOUT_STABLE
 8             | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
 9             | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
10             | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
11             | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
12             | View.SYSTEM_UI_FLAG_IMMERSIVE);
13 }
14
15 // This snippet shows the system bars. It does this by removing all the flags
16 // except for the ones that make the content appear under the system bars.
17 private void showSystemUI() {
18     mDecorView.setSystemUiVisibility(
19             View.SYSTEM_UI_FLAG_LAYOUT_STABLE
20             | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
21             | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
22 }

Note: If you like the auto-hiding behavior of IMMERSIVE_STICKY but need to show your own UI controls as well, just use IMMERSIVE combined with Handler.postDelayed() or something similar to re-enter immersive mode after a few seconds.

Current Requirement:

  • Hide navigation bar on startup
  • Auto hide navigation bar (IMMERSIVE_STICKY + poseDelayed() )

Implementation:

 1     //////////////////////////////////////////////////////////////////////////
 2     public void hideNavigationBar() {
 3         int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
 4             | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
 5             | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
 6             | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
 7             | View.SYSTEM_UI_FLAG_FULLSCREEN; // hide status bar
 8
 9         if( android.os.Build.VERSION.SDK_INT >= 19 ){ 10             uiFlags |= 0x00001000;    //SYSTEM_UI_FLAG_IMMERSIVE_STICKY: hide navigation bars - compatibility: building API level is lower thatn 19, use magic number directly for higher API target level
11         } else {
12             uiFlags |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
13         }
14
15         getWindow().getDecorView().setSystemUiVisibility(uiFlags);
16     }
17
18
19     //////////////////////////////////////////////////////////////////////////
20     @Override
21     protected void onCreate (Bundle savedInstanceState) {
22         ...
23         hideNavigationBar();
24         super.onCreate(savedInstanceState);
25         ...
26     }
27
28     //////////////////////////////////////////////////////////////////////////
29     @Override
30     public void onWindowFocusChanged(boolean hasFocus) {
31         super.onWindowFocusChanged(hasFocus);
32         if( hasFocus ) {
33             hideNavigationBar();
34         }
35     }

Note: postDelayed is not yet added, because on tested devices, navigation bar will auto hide for some secs.

Add postDelayed() event to manully auto hide nav bar if needed.

时间: 2024-10-23 11:19:10

[工作积累] Android: Hide Navigation bar 隐藏导航条的相关文章

Android判断Navigation Bar 是否透明

设置Navigation Bar 透明 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); 或者Theme中添加  <item name="android:windowTranslucentNavigation">true</item> /** * Convenience function to set the flag bits as specified i

导航右侧添加按钮、隐藏导航条和状态栏

1.添加导航右侧按钮 UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"无所谓"style:UIBarButtonItemStyleDone target:self action:@selector(clickRightButton)]; [self.navigationItem setRightBarButtonItem:rightButton]; 2.隐藏导航条 navigationBar

navigationcontroller pop回到隐藏导航条页面出现黑块问题

如果隐藏导航条的页面使用无动画模式隐藏导航条 [self.navigationController setNavigationBarHidden:YES animated:NO];在导航pop回来时就会出现黑块问题. 将隐藏导航条设置为动画模式:[self.navigationController setNavigationBarHidden:YES animated:YES]; 就能解决这个问题.

把 Navigation Bar 下面那条线删掉的最简单的办法! — By: 昉

系统默认的 Navigation Bar 下面一直有条线,翻尽了文档却没找到能把它弄走的相关接口,处女座的简直木法忍啊有木有!!!! 研究了一下navigationBar下的子视图,原来只需要几行代码就可以轻松把它搞掉~: for (UIView *view in self.navigationController.navigationBar.subviews) { if ([view isMemberOfClass:NSClassFromString(@"_UINavigationBarBac

iOS隐藏导航条1px的底部横线

默认情况下会有这条线 第一种方法: UINavigationBar *navigationBar = self.navigationController.navigationBar; // white.png图片自己下载个纯白色的色块,或者自己ps做一个 [navigationBar setBackgroundImage:[UIImage imageNamed:@"white.png"]                    forBarPosition:UIBarPositionAn

【转】iOS隐藏导航条1px的底部横线

默认情况下会有这条线 第一种方法: 1 2 3 4 5 6 UINavigationBar *navigationBar = self.navigationController.navigationBar; // white.png图片自己下载个纯白色的色块,或者自己ps做一个 [navigationBar setBackgroundImage:[UIImage imageNamed:@"white.png"]                    forBarPosition:UIB

[工作积累] Android dynamic library &amp; JNI_OnLoad

Bionic libc doesn't load dependencies for current .so file (diff from Windows or Linux) so a explicit calling of Java's System.loadLibrary() is needed, in order to load depedency libraries. otherwise the original .so will fail to load. JNI_OnLoad wil

[工作积累] android 中添加libssl和libcurl

1. libssl https://github.com/guardianproject/openssl-android 然后执行ndk-build 2.libcurl 源代码组织结构, 下面的makefile要依赖这个结构. 因为curl依赖openssl, 所以需要先编译openssl: src/openssl-android src/curl-android 下载curl的代码, 并放入src/curl-android/jni 下. 本来想用cygwin来运行configure, 生成an

[工作积累] Android system dialog with native callback

JNI: invoke java dialog with native callback: store native function address in java, and invoke native another method to this function. key points: 1.Store callback function pointer(address) in java: to support multiple calls, use data block for each