android设置沉浸式状态栏

转自:http://blog.csdn.net/zuiwuyuan/article/details/50100093

Android 使用SystemBarTint设置状态栏颜色


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

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

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

    

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


1.  引入类库

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

    

[plain]  view plain   copy
  print ?
  1. dependencies {  
  2.     compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'  
  3. }  

2.  在Activity中添加方法:

[java]  view plain   copy
  print ?
  1. /** 
  2.      * Apply KitKat specific translucency. 
  3.      */  
  4.     private void applyKitKatTranslucency() {  
  5.   
  6.         // KitKat translucent navigation/status bar.  
  7.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {  
  8.             setTranslucentStatus(true);  
  9.             SystemBarTintManager mTintManager = new SystemBarTintManager(this);  
  10.             mTintManager.setStatusBarTintEnabled(true);  
  11.   
  12.             mTintManager.setStatusBarTintResource(R.color.colorTop);//通知栏所需颜色  
  13.         }  
  14.   
  15.     }  
  16.   
  17.     @TargetApi(19)  
  18.     private void setTranslucentStatus(boolean on) {  
  19.         Window win = getWindow();  
  20.         WindowManager.LayoutParams winParams = win.getAttributes();  
  21.         final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;  
  22.         if (on) {  
  23.             winParams.flags |= bits;  
  24.         } else {  
  25.             winParams.flags &= ~bits;  
  26.         }  
  27.         win.setAttributes(winParams);  
  28.     }  

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

[java]  view plain   copy
  print ?
  1. @Override  
  2.     protected void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         setContentView(R.layout.activity_main);  
  5.   
  6.         applyKitKatTranslucency();  
  7.     }  


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

[html]  view plain   copy
  print ?
  1. <!-- 去掉tab顶部的黑边 -->  
  2.    <style name="no_title" parent="@android:style/Theme.Light.NoTitleBar">        
  3.   
  4.        <!-- 沉浸式状态栏 -->  
  5.        <item name="android:fitsSystemWindows">true</item>  
  6.        <item name="android:clipToPadding">false</item>  
  7.    </style>  

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

[html]  view plain   copy
  print ?
  1.  <application  
  2.         android:name=".activity.base.MyApp"  
  3.         android:allowBackup="true"  
  4.         android:icon="@drawable/ic_launcher"  
  5.         android:label="@string/app_name"  
  6.         android:persistent="true"  
  7.         android:theme="@style/no_title">  
  8. </application>  


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

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


猜你喜欢

转载自blog.csdn.net/u012049463/article/details/73730207