[Transfer] Android implements the hiding and display function of the system status bar
#Problem description Especially for video apps, it is necessary to hide the system status bar and display it in full screen after switching to horizontal screen, so that you can watch videos with a larger screen. when switching back to After the screen is turned vertically, the status bar is displayed again. So how to achieve it?
There are many methods circulating on the Internet
for example
-
Modify
themein theAndroidManifest.xmlfile to -
Execute the following code before the
setContentViewmethod: -
Through the
setSystemUiVisibilitymethod ofView -
Use the following code to hide and display the status bar: In my project, I want to achieve the following requirements: in the current
Activity, after switching to landscape screen,Activitycannot be destroyed and re-initialized, and the system status bar can be hidden and displayed in full screen; after switching back to vertical screen, the status bar can be displayed again. Also, I don’t need to hide the title bar.
Therefore, neither method 1 nor 2 is suitable for me. Method 3, which I have used, calls the setSystemUiVisibility method. The parameters passed in by this method can be:
- View.SYSTEM_UI_FLAG_VISIBLE: Display the status bar, and the Activity is not displayed in full screen (returning to the normal state with state).
- View.INVISIBLE: Hide the status bar and the Activity will stretch to full screen.
- View.SYSTEM_UI_FLAG_FULLSCREEN: Activity is displayed in full screen, and the status bar is hidden and covered.
- View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN: The Activity is displayed in full screen, but the status bar will not be hidden and covered. The status bar is still visible, and the top layout part of the Activity will be covered by the status.
- View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION: The effect is the same as View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
- View.SYSTEM_UI_LAYOUT_FLAGS: The effect is the same as View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
- View.SYSTEM_UI_FLAG_HIDE_NAVIGATION: Hide virtual buttons (navigation bar). Some mobile phones use virtual buttons instead of physical buttons.
- View.SYSTEM_UI_FLAG_LOW_PROFILE: The status bar display is in a low-power display state (low profile mode), and some icons on the status bar will be hidden.
What I need to pass in here is View.SYSTEM_UI_FLAG_FULLSCREEN, but when I pass in this parameter, the result is: just the status bar disappears, but the position is still there. (Test mobile phones: Huawei Honor 8 system is EMUI 5.0 based on Android 7.0; Samsung Galaxy S6 system is Android 6.0)
Finally, use method 4 to successfully meet the needs.
Summarize
The above is the Android implementation of hiding and displaying the system status bar function introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for your support of the ZaLou.Cn website!
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
requestWindowFeature(Window.FEATURE_NO_TITLE)
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
3、通过View的setSystemUiVisibility方法
4、通过如下代码实现状态栏的隐藏和显示:
在我的项目中是要实现如下需求:在当前Activity中,切换到横屏后,不能销毁Activity再重新初始化,并且实现隐藏系统状态栏,全屏显示;当切换回竖屏后,又显示状态栏。另外,我不需要隐藏标题栏。
因此,方法1、2均不适合我。方法3,我采用过,调用setSystemUiVisibility方法,该方法传入的参数可以为:
- View.SYSTEM_UI_FLAG_VISIBLE:显示状态栏,Activity不全屏显示(恢复到有状态的正常情况)。
- View.INVISIBLE:隐藏状态栏,同时Activity会伸展全屏显示。
- View.SYSTEM_UI_FLAG_FULLSCREEN:Activity全屏显示,且状态栏被隐藏覆盖掉。
- View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN:Activity全屏显示,但状态栏不会被隐藏覆盖,状态栏依然可见,Activity顶端布局部分会被状态遮住。
- View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION:效果同View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
- View.SYSTEM_UI_LAYOUT_FLAGS:效果同View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
- View.SYSTEM_UI_FLAG_HIDE_NAVIGATION:隐藏虚拟按键(导航栏)。有些手机会用虚拟按键来代替物理按键。
- View.SYSTEM_UI_FLAG_LOW_PROFILE:状态栏显示处于低能显示状态(low profile模式),状态栏上一些图标显示会被隐藏。
这里我需要传入的是View.SYSTEM_UI_FLAG_FULLSCREEN,可是当我传入该参数后,结果是:只是状态栏消失了,但是位置还在。(测试手机:华为荣耀8 系统是基于Android 7.0的EMUI 5.0;三星galaxy s6 系统是Android 6.0)
最后,使用方法4,成功满足需求。
总结
以上所述是小编给大家介绍的Android实现系统状态栏的隐藏和显示功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对ZaLou.Cn网站的支持!
参考
https://cloud.tencent.com/developer/article/1741895