zoukankan      html  css  js  c++  java
  • Android实现沉浸式状态栏(必须高逼格)

     情况一:保留状态栏 只是将我们的布局嵌入到状态栏中

    方法一:通过设置theme主题

    因为 API21 之后(也就是 android 5.0 之后)的状态栏,会默认覆盖一层半透明遮罩。且为了保持4.4以前系统正常使用,故需要三份 style 文件,即默认的values(不设置状态栏透明)、values-v19、values-v21(解决半透明遮罩问题)。

    //valuse
    <style name="TranslucentTheme" parent="AppTheme">
    </style>
    
    // values-v19。v19 开始有 android:windowTranslucentStatus 这个属性
    <style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="android:windowTranslucentStatus">true</item>
            <item name="android:windowTranslucentNavigation">true</item>
    </style>
    
    // values-v21。5.0 以上提供了 setStatusBarColor()  方法设置状态栏颜色。
    <style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">false</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

    然后在清单文件中将theme设置给对应的Activity即可。

    方法二:代码设置

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.flags |= flagTranslucentNavigation;
            window.setAttributes(attributes);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        } else {
            Window window = getWindow();
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation;
            window.setAttributes(attributes);
        }
    }

    注:但是从图片中也看到了,该方案会导致一个问题就是导航栏颜色变灰。经测试,在5.x以下导航栏透明是可以生效的,但是5.x以上的导航栏会变灰色(正常情况下我们期望导航栏保持默认黑色不变),但是因为设置了FLAG_TRANSLUCENT_NAVIGATION,所以即时代码中设置了getWindow().setNavigationBarColor(Color.BLACK);此时也是不起作用的。但是如果不设置该FLAG,状态栏又无法设置为隐藏和设置透明。综上还是推荐第一种。

    情况二:去掉状态栏

    方法一:代码设置隐藏状态栏

    /**
     * 通过设置全屏,设置状态栏透明
     *
     * @param activity
     */
    private void fullScreen(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色
                Window window = activity.getWindow();
                View decorView = window.getDecorView();
                //两个 flag 要结合使用,表示让应用的主体内容占用系统状态栏的空间
                int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
                decorView.setSystemUiVisibility(option);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);
                //导航栏颜色也可以正常设置
    //                window.setNavigationBarColor(Color.TRANSPARENT);
            } else {
                Window window = activity.getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
                int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
                attributes.flags |= flagTranslucentStatus;
    //                attributes.flags |= flagTranslucentNavigation;
                window.setAttributes(attributes);
            }
        }
    }

    最后 附上大神的博客和github

    http://www.jianshu.com/p/dc20e98b9a90

    https://github.com/yazhi1992/Practice

  • 相关阅读:
    [转载][mysql]mysql字符集干货
    [mysql]修改表段默认值
    微信支付之h5方式(非微信内置浏览器中支付)
    阿里云 ECS 安全组
    Memcached cas 陷阱
    Memcached 分布式集群
    nginx 配置多个主机
    static类型的变量
    全局变量和局部变量
    nginx 负载均衡(默认算法)
  • 原文地址:https://www.cnblogs.com/yegong0214/p/7872831.html
Copyright © 2011-2022 走看看