1、状态栏显示在图片的上面,如图所示:
实现方法:重写Activty:onWindowFocusChanged()
(1) 适用于首页,刚运行程序时,状态栏悬浮在首页的上面
int option=View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
(2) 适用于全屏显示,例如视频播放(横屏)
int option = 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
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && Build.VERSION.SDK_INT >= 19) {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
View decorView = getWindow().getDecorView();
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);
}
}
2、状态栏与布局垂直排布,互不遮挡。如图:
实现方法:
layout布局文件:根布局设置fitsSystemWindows属性为true
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xl.test.activity.MainActivity"
android:fitsSystemWindows="true">
<include layout="@layout/commont_toobar" />
</LinearLayout>
values/styles.xml:
<resources>
<style name="ColorTranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"/>
</resources>
values-v19/styles.xml:
<resources>
<style name="ColorTranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
</resources>
values-v21/styles.xml:statusBarColor 颜色与主题颜色一致
<resources>
<style name="ColorTranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:statusBarColor">@color/dividerColor</item>
</style>
</resources>
AndroidManitest.xml: 设置Theme属性
<activity android:name=".activity.MainActivity"
android:theme="@style/ColorTranslucentTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
原文链接:http://blog.youkuaiyun.com/lizhiying61f/article/details/52213585
http://blog.youkuaiyun.com/guolin_blog/article/details/51763825