沉浸式状态栏的两种表现形态

来源:互联网 发布:java 判断是不是汉字 编辑:程序博客网 时间:2024/03/29 19:49

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.csdn.net/lizhiying61f/article/details/52213585
http://blog.csdn.net/guolin_blog/article/details/51763825

原创粉丝点击