淡化系统状态栏
设置系统状态栏为透明,隐藏actionbar,内容不会冲到状态栏之下。
<style name="Theme.MainActivity" parent="AppTheme.NoActionBar"><item name="windowActionBar">false</item><item name="windowNoTitle">true</item><!--statusbar透明--><item name="android:windowTranslucentStatus">true</item></style>
这时,系统状态栏下的颜色是由应用主题的主色中的colorPrimaryDark决定
<!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"><!-- Customize your theme here. --><!--<item name="colorPrimary">@color/colorPrimary</item>--><item name="colorPrimary">@color/colorPrimary</item><item name="colorPrimaryDark">@color/colorPrimaryDark</item><item name="colorAccent">@color/colorAccent</item></style>
隐藏系统状态栏
针对Android4.1(API Level 16)之下的版本:
//隐藏系统状态栏// If the Android version is lower than Jellybean, use this call to hide// the status bar.if (Build.VERSION.SDK_INT < 16) {getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);}
针对Android4.1(Api Level16)及之上版本:
//在Android 4.1(API level 16)以及以上的版本中,你可以使用setSystemUiVisibility())来进行动态隐藏View decorView = getWindow().getDecorView();// Hide the status bar.int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;decorView.setSystemUiVisibility(uiOptions);// Remember that you should never show the action bar if the// status bar is hidden, so hide that too if necessary.
官方建议同时隐藏掉ActionBar,如果不隐藏ActionBar将突兀的出现在状态栏之下内容之上。
注:跳转到另一个Activity、按下Home键会导致设置的系统UI的标签被清除,系统状态栏会重现显示。点击了ActionBar的menu菜单,状态栏也会重新出现。如果想要避免这种状况,就不要在onCreate() 方法中设置UI标签,而应该在onResume()与onWindowFocusChaned()里设定UI标签。
全屏显示内容:
设置Activity的主题
<item name="android:windowFullscreen">true</item>
代码设置的方式:在onCreate()的setContentView()之前调用
/*全屏*/requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题栏getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
使用非粘性沉浸模式:
/*** 非粘性沉浸模式,在不改变内容区域大小的情况下,隐藏与显示状态栏和导航栏。但是状态栏占得位置还在*/private void unStickyModeFullScreen() {/*非粘性沉浸模式,在不改变内容区域大小的情况下,隐藏与显示状态栏和导航栏。和上面的效果基本相同,但是同时隐藏了ActionBar*/View mDecorView = getWindow().getDecorView();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);}
指定statusBar的颜色:
在主题中添加,首先设置其颜色不跟随ToolBar(ActionBar),android:windowTranslucentStatus位false时,内容不会冲到statusBar的下面,为true时会占据statusbar的位置。
<!--statusbar颜色跟随ToolBar和ActionBar--><item name="android:windowTranslucentStatus">false</item><!--statusbar颜色--><item name="android:statusBarColor">@color/black_272a33</item>
1147

被折叠的 条评论
为什么被折叠?



