全屏一,代码方式
@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
<style name="AppTheme.FullScreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
沉浸式1,TranslucentTheme
为页面指定主题样式
<activity
android:name="LoginActivity"
android:screenOrientation="portrait"
android:theme="@style/TranslucentTheme" />
style.xml
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"/>
style.xml(v19)
<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)
<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>
沉浸式2,Java的方式
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的高度)用于填充高度