zoukankan      html  css  js  c++  java
  • Android 使用SystemBarTint设置状态栏颜色

       做项目时,发现APP的状态栏是系统默认的颜色,突然想到,为啥别的APP是自己设置的颜色(和APP本身很相搭),于是也想给自己的APP设置系统状态栏的颜色,更加美美哒。。。

      搜了下,发现原来设置状态栏居然有个很高大上的名字(听不懂的都是高大上)——沉浸式状态栏,Android4.4以后开始支持沉浸式状态栏, 继续搜索,发现,有一个很简单的开源项目——SystemBarTint,可以很完美的支持沉浸式状态栏。

        SystemBarTint地址: https://github.com/hexiaochun/SystemBarTint

        

    下面,简单演示下如何使用该库,首先,先看下效果,有图才有真相:


    1.  引入类库

        使用Android Studio,直接在build.gradle文件中引入库: 

        

    dependencies {
        compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
    }

       使用Eclipse,可下载JAR包,并引入到项目的libs文件夹中。


    2.  在Activity中添加方法:

    /**
         * Apply KitKat specific translucency.
         */
        private void applyKitKatTranslucency() {
    
            // KitKat translucent navigation/status bar.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                setTranslucentStatus(true);
                SystemBarTintManager mTintManager = new SystemBarTintManager(this);
                mTintManager.setStatusBarTintEnabled(true);
    
                mTintManager.setStatusBarTintResource(R.color.colorTop);//通知栏所需颜色
            }
    
        }
    
        @TargetApi(19)
        private void setTranslucentStatus(boolean on) {
            Window win = getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            if (on) {
                winParams.flags |= bits;
            } else {
                winParams.flags &= ~bits;
            }
            win.setAttributes(winParams);
        }

    然后, 在OnCreate()方法中调用applyKitKatTranslucency方法:

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            applyKitKatTranslucency();
        }


    3.  在style.xml中,添加系统的样式:

     <!-- 去掉tab顶部的黑边 -->
        <style name="no_title" parent="@android:style/Theme.Light.NoTitleBar">      
    
            <!-- 沉浸式状态栏 -->
            <item name="android:fitsSystemWindows">true</item>
            <item name="android:clipToPadding">false</item>
        </style>

    当然了,别忘了在AndroidManifest.xml进行配置主题:

     <application
            android:name=".activity.base.MyApp"
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:persistent="true"
            android:theme="@style/no_title">
    </application>


    注: 这个是必要的,如果不添加,会造成一些页面的变形。

    综上, 便可以在4.4以上的系统中方便的设置状态栏颜色,有木有感觉你的APP变得更好看了呢!


    扩展:http://www.jianshu.com/p/e1c937000343

  • 相关阅读:
    面向对象方法的重载(overloading)和覆盖(overriding)。
    注意:在对象变量中存放的是引用(地址);在简单变量中存放的是数值。
    类方法中的一类特殊方法:构造方法。
    在用面向对象思想开发的过程中,可以复用对象就进行复用,如无法进行复用则开发新的对象。
    对于对象的要求:高内聚、低耦合,这样容易拼装成为一个系统。
    为什么要使用面向对象:
    什么是对象:EVERYTHING IS OBJECT(万物皆对象)
    Java如何在指定端口创建套接字?
    Java如何查找系统的代理设置?
    Java如何检查端口是否被使用?
  • 原文地址:https://www.cnblogs.com/hehe520/p/6329952.html
Copyright © 2011-2022 走看看