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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值