【Android】Activity进入沉浸模式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【Android】Activity进入沉浸模式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6117字,纯文字阅读大概需要9分钟。
内容图文
![【Android】Activity进入沉浸模式](/upload/InfoBanner/zyjiaocheng/1077/e451730124254c1492cb472d0f8eea37.jpg)
沉浸模式-ImmersiveMode:观影时,看图时常见常用。即全屏模式。
在4.4里面,底部虚拟键出现,代表手机为Nexus5.我的应用被底部虚拟键挤压变形,遮挡关键操作按钮。
比如在我的欢迎页,有张圆形的图片,被压扁了,这时,我选择将底部虚拟键隐藏。
1,打开沉浸模式。
1 /** 2 * Detects and toggles immersive mode (also known as "hidey bar" mode). 3 */ 4 public void toggleHideyBar() { 5 6 // BEGIN_INCLUDE (get_current_ui_flags) 7 // The UI options currently enabled are represented by a bitfield. 8 // getSystemUiVisibility() gives us that bitfield. 9 int uiOptions = getActivity().getWindow().getDecorView().getSystemUiVisibility(); 10int newUiOptions = uiOptions; 11// END_INCLUDE (get_current_ui_flags) 12// BEGIN_INCLUDE (toggle_ui_flags)13boolean isImmersiveModeEnabled = 14 ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions); 15if (isImmersiveModeEnabled) { 16 Log.i(TAG, "Turning immersive mode mode off. "); 17 } else { 18 Log.i(TAG, "Turning immersive mode mode on."); 19 } 2021// Navigation bar hiding: Backwards compatible to ICS.22if (Build.VERSION.SDK_INT >= 14) { 23 newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; 24 } 2526// Status bar hiding: Backwards compatible to Jellybean27if (Build.VERSION.SDK_INT >= 16) { 28 newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN; 29 } 3031// Immersive mode: Backward compatible to KitKat. 32// Note that this flag doesn‘t do anything by itself, it only augments the behavior 33// of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample 34// all three flags are being toggled together. 35// Note that there are two immersive mode UI flags, one of which is referred to as "sticky". 36// Sticky immersive mode differs in that it makes the navigation and status bars 37// semi-transparent, and the UI flag does not get cleared when the user interacts with 38// the screen.39if (Build.VERSION.SDK_INT >= 18) { 40 newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; 41 } 4243 getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions); 44//END_INCLUDE (set_ui_flags)45 }
2,状态变化监听器
final View decorView = getActivity().getWindow().getDecorView(); decorView.setOnSystemUiVisibilityChangeListener( new View.OnSystemUiVisibilityChangeListener() { @Override publicvoid onSystemUiVisibilityChange(int i) { int height = decorView.getHeight(); Log.i(TAG, "Current height: " + height); } });
3,五种状态Flag
// BEGIN_INCLUDE (get_current_ui_flags) // The "Decor View" is the parent view of the Activity. It‘s also conveniently the easiest // one to find from within a fragment, since there‘s a handy helper method to pull it, and // we don‘t have to bother with picking a view somewhere deeper in the hierarchy and calling // "findViewById" on it. View decorView = getActivity().getWindow().getDecorView(); int uiOptions = decorView.getSystemUiVisibility(); int newUiOptions = uiOptions; // END_INCLUDE (get_current_ui_flags) // BEGIN_INCLUDE (toggle_lowprofile_mode) // Low profile mode doesn‘t resize the screen at all, but it covers the nav & status bar // icons with black so they‘re less distracting. Unlike "full screen" and "hide nav bar," // this mode doesn‘t interact with immersive mode at all, but it‘s instructive when running // this sample to observe the differences in behavior.if (mLowProfileCheckBox.isChecked()) { newUiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;//低调模式:通知栏和虚拟键变暗 } else { newUiOptions &= ~View.SYSTEM_UI_FLAG_LOW_PROFILE; } // END_INCLUDE (toggle_lowprofile_mode) // BEGIN_INCLUDE (toggle_fullscreen_mode) // When enabled, this flag hides non-critical UI, such as the status bar, // which usually shows notification icons, battery life, etc // on phone-sized devices. The bar reappears when the user swipes it down. When immersive // mode is also enabled, the app-drawable area expands, and when the status bar is swiped // down, it appears semi-transparently and slides in over the app, instead of pushing it // down.if (mHideStatusBarCheckBox.isChecked()) {//全屏模式:隐藏状态栏,但并不隐藏虚拟键 newUiOptions |= View.SYSTEM_UI_FLAG_FULLSCREEN; } else { newUiOptions &= ~View.SYSTEM_UI_FLAG_FULLSCREEN; } // END_INCLUDE (toggle_fullscreen_mode) // BEGIN_INCLUDE (toggle_hidenav_mode) // When enabled, this flag hides the black nav bar along the bottom, // where the home/back buttons are. The nav bar normally instantly reappears // when the user touches the screen. When immersive mode is also enabled, the nav bar // stays hidden until the user swipes it back.if (mHideNavCheckbox.isChecked()) {//隐藏虚拟键,点击可出现 newUiOptions |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; } else { newUiOptions &= ~View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; } // END_INCLUDE (toggle_hidenav_mode) // BEGIN_INCLUDE (toggle_immersive_mode) // Immersive mode doesn‘t do anything without at least one of the previous flags // enabled. When enabled, it allows the user to swipe the status and/or nav bars // off-screen. When the user swipes the bars back onto the screen, the flags are cleared // and immersive mode is automatically disabled.if (mImmersiveModeCheckBox.isChecked()) { newUiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE; } else { newUiOptions &= ~View.SYSTEM_UI_FLAG_IMMERSIVE; } // END_INCLUDE (toggle_immersive_mode) // BEGIN_INCLUDE (toggle_immersive_mode_sticky) // There‘s actually two forms of immersive mode, normal and "sticky". Sticky immersive mode // is different in 2 key ways: //// * Uses semi-transparent bars for the nav and status bars // * This UI flag will *not* be cleared when the user interacts with the UI. // When the user swipes, the bars will temporarily appear for a few seconds and then // disappear again.if (mImmersiveModeStickyCheckBox.isChecked()) { newUiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; } else { newUiOptions &= ~View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; } // END_INCLUDE (toggle_immersive_mode_sticky) // BEGIN_INCLUDE (set_ui_flags) //Set the new UI flags. decorView.setSystemUiVisibility(newUiOptions); Log.i(TAG, "Current height: " + decorView.getHeight() + ", width: " + decorView.getWidth()); // END_INCLUDE (set_ui_flags)
原文地址:http://www.cnblogs.com/bvin/p/3485174.html
原文:http://www.cnblogs.com/outOfview/p/3672581.html
内容总结
以上是互联网集市为您收集整理的【Android】Activity进入沉浸模式全部内容,希望文章能够帮你解决【Android】Activity进入沉浸模式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。