android mode 全屏_Android 4.4新特性之开启全屏沉浸模式

本文介绍了Android 4.4 KitKat引入的全屏沉浸模式,该模式下应用界面会占据整个屏幕,隐藏状态栏和导航栏,增强大屏体验。通过设置View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY等标志,可以实现全屏并控制状态栏和导航栏的显示与隐藏。详细讲述了普通全屏、沉浸模式和黏性沉浸模式的差异以及相关代码实现。
摘要由CSDN通过智能技术生成

作为Android4.4 KitKat系统的新特性之一“Full-screen Immersive Mode(全屏沉浸模式)”。当启用该模式,应用程序的界面将占据整个屏幕,系统自动将隐藏系统的状态栏和导航栏,让应用程序内容可以在最大显示范围呈现,增加大屏体验,而当需要查看通知的时候只需要从顶部向下滑动就能呼出通知栏。

普通全屏模式(Fullscreen)、沉浸模式 (Immersive)、黏性沉浸模式 (Sticky Immersive)

说到全屏,我们先看看如何实现普通的全屏模式。

参考资料:

接下来我们一起看看如何实现Android4.4系统带来的全屏沉浸模式。

参考资料:

官方文档上说全屏沉浸模式有两种选择,一种是

View.SYSTEM_UI_FLAG_IMMERSIVE

这个选择通过组合

View.SYSTEM_UI_FLAG_FULLSCREEN

View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

将这个组合通过

setSystemUiVisibility

设置以后可以实现全屏沉浸模式,并且隐藏顶部状态栏和底部的虚拟导航键。但是这个选择通过顶部下滑就恢复原状,同时自动清除这三个标志。

另一种是

View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY

同样设置以后可以实现全屏沉浸模式,并且隐藏顶部状态栏和底部的虚拟导航键。与第一种不同的是此时通过顶部下滑会显示出透明的状态栏和虚拟导航栏,在一段时间以后自动回复成全屏沉浸模式,需要手动清除这三个标志回复原状。

最后做一下扩展

//This snippet hides the system bars.

private voidhideSystemUI() {//Set the IMMERSIVE flag.//Set the content to appear under the system bars so that the content//doesn't resize when the system bars hide and show.

mDecorView.setSystemUiVisibility(

View.SYSTEM_UI_FLAG_LAYOUT_STABLE|View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION //hide nav bar

| View.SYSTEM_UI_FLAG_FULLSCREEN //hide status bar

|View.SYSTEM_UI_FLAG_IMMERSIVE);

}//This snippet shows the system bars. It does this by removing all the flags//except for the ones that make the content appear under the system bars.

private voidshowSystemUI() {

mDecorView.setSystemUiVisibility(

View.SYSTEM_UI_FLAG_LAYOUT_STABLE|View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

}

@Overridepublic voidonWindowFocusChanged(boolean hasFocus) {

super.onWindowFocusChanged(hasFocus);if(hasFocus) {

decorView.setSystemUiVisibility(

View.SYSTEM_UI_FLAG_LAYOUT_STABLE|View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_FULLSCREEN|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);}

}

注意标志位的版本判断:

public voidtoggleHideyBar() {//BEGIN_INCLUDE (get_current_ui_flags)//The UI options currently enabled are represented by a bitfield.//getSystemUiVisibility() gives us that bitfield.

int uiOptions =getActivity().getWindow().getDecorView().getSystemUiVisibility();int newUiOptions =uiOptions;//END_INCLUDE (get_current_ui_flags)//BEGIN_INCLUDE (toggle_ui_flags)//boolean isImmersiveModeEnabled =//((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);

boolean isImmersiveModeEnabled =((uiOptions| View.SYSTEM_UI_FLAG_IMMERSIVE) ==uiOptions);if(isImmersiveModeEnabled) {

Log.i(TAG,"Turning immersive mode mode off.");

}else{

Log.i(TAG,"Turning immersive mode mode on.");

}//Navigation bar hiding: Backwards compatible to ICS.

if (Build.VERSION.SDK_INT >= 14) {

newUiOptions^=View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;

}//Status bar hiding: Backwards compatible to Jellybean

if (Build.VERSION.SDK_INT >= 16) {

newUiOptions^=View.SYSTEM_UI_FLAG_FULLSCREEN;

}//Immersive mode: Backward compatible to KitKat.//Note that this flag doesn't do anything by itself, it only augments the behavior//of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample//all three flags are being toggled together.//Note that there are two immersive mode UI flags, one of which is referred to as "sticky".//Sticky immersive mode differs in that it makes the navigation and status bars//semi-transparent, and the UI flag does not get cleared when the user interacts with//the screen.

if (Build.VERSION.SDK_INT >= 18) {//newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

newUiOptions ^=View.SYSTEM_UI_FLAG_IMMERSIVE;

}

getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions);//END_INCLUDE (set_ui_flags)

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值