android set immersive status bar

Transfer from: http://blog.csdn.net/zuiwuyuan/article/details/50100093

Android uses SystemBarTint to set the status bar color


When working on a project, I found that the status bar of the APP is the default color of the system. I suddenly thought, why other APPs are the color set by myself (it matches the APP itself), so I also want to set the color of the system status bar for my APP , More beautiful. . .

  I searched it and found that the original setting status bar actually had a very tall name (all those who don’t understand are tall and tall)-immersive status bar, Android 4.4 began to support immersive status bar, continue to search, found that, There is a very simple open source project-SystemBarTint, which can perfectly support the immersive status bar.

    SystemBarTint address:  https://github.com/hexiaochun/SystemBarTint

    

Below, a brief demonstration of how to use the library, first of all, let's look at the effect, there is a picture to have the truth:


1. Introduce class library

    Using Android  Studio, directly import the library in the build.gradle file: 

    

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

2. Add a method in 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); //The required color of the notification bar  
  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>  


Note: This is necessary, if it is not added, it will cause some page distortion.

In summary, you can easily set the status bar color in the system above 4.4, and you may feel that your APP has become better!


Guess you like

Origin blog.csdn.net/u012049463/article/details/73730207