[TOC]

概述

设置全屏

使用代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hideActionStatusBar();
hideBottomStatusBar();
setContentView(R.layout.activity_main);
}

/**
* 隐藏ActionBar和StatusBar
*/
private void hideActionStatusBar() {
//set no title bar 需要在setContentView之前调用
requestWindowFeature(Window.FEATURE_NO_TITLE);
//no status bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//特殊情况下
if (getSupportActionBar()!=null) getSupportActionBar().hide();
if (getActionBar()!=null) getActionBar().hide();
}

/**
* 隐藏 NavigationBar和StatusBar
*/
protected void hideBottomStatusBar() {
//隐藏虚拟按键,并且全屏
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
View v = this.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
//for new api versions.
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
}

使用Style文件

1
2
3
4
<style name="AppTheme.FullScreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>

设置沉浸式

使用style样式

为页面指定主题样式

1
2
3
4
<activity
android:name="LoginActivity"
android:screenOrientation="portrait"
android:theme="@style/TranslucentTheme" />

设置style.xml

1
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"/>

style.xml(v19)

1
2
3
4
5
6
7
8
9
10
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>

<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
</resources>

style.xml(v21)

1
2
3
4
5
6
7
8
9
10
11
12
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>

<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>

使用代码

1
2
3
4
5
6
7
8
9
10
11
12
13
public static void transparentStatusBar(@NonNull final Window window) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
int option = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
int vis = window.getDecorView().getSystemUiVisibility();
window.getDecorView().setSystemUiVisibility(option | vis);
window.setStatusBarColor(Color.TRANSPARENT);
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}

fitsSystemWindows
android:fitsSystemWindows=“true” 在xml中设置这个属性,可以在沉浸式中预留statusBar的高度。

不同的Fragment使用不同的status颜色
可以使用沉浸式,在Fragment的xml中顶部预留30dp(或者动态设置status的高度)用于填充高度