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變得更好看了呢!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章