安卓透明狀態欄小技巧

隨着安卓4.4的發佈,再也不用看到那些黑黑的狀態欄了,狀態欄也可以實現透明和自定義顏色了,學會狀態欄透明確實會讓你的APP好看不少。因爲誰能忍受那些大黑邊 。加上5.0md的發佈,靈活使用狀態欄配色可以表達各式各樣的主題心情,所以就是一句話,狀態欄配色雖然細小但是卻顯得十分重要了。


轉載自zhangphil大神的博客,轉載我就註明原文鏈接---http://blog.csdn.NET/zhangphil;

原本有三篇文章,我就融合了下當一篇文章發佈了。希望讀後能有所啓發吧。


老規矩,來句名言點綴下-------

真正的美德不可沒有實用的智慧,而實用的智慧也不可沒有美德。

——— 亞里士多德


一:用style樣式實現透明狀態欄

(1) 我的例子中,Androidmanifest.xml文件中定義的app的style爲AppTheme:




[html] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     package="zhangphil.myapplication">  

  4.   

  5.     <application  

  6.         android:allowBackup="true"  

  7.         android:icon="@mipmap/ic_launcher"  

  8.         android:label="@string/app_name"  

  9.         android:supportsRtl="true"  

  10.         android:theme="@style/AppTheme">  

  11.         <activity android:name=".MainActivity">  

  12.             <intent-filter>  

  13.                 <action android:name="android.intent.action.MAIN" />  

  14.   

  15.                 <category android:name="android.intent.category.LAUNCHER" />  

  16.             </intent-filter>  

  17.         </activity>  

  18.     </application>  

  19.   

  20. </manifest>  




上面的Androidmanifest.xml是Android Studio自動生成的,同時Android Studio自動在res/values目錄下生成的styles.xml文件中定義了AppTheme,我把這個AppTheme重新修改爲:


[html] view plain copy

  1. <resources>  

  2.   

  3.     <style name="AppTheme" parent="android:Theme.Holo.Light.NoActionBar">  

  4.   

  5.     </style>  

  6.   

  7. </resources>  




(2)再爲Android v19準備一套styles.xml文件。在res/目錄下新建一個名爲values-v19目錄,在res/values-v21目錄下再建一個styles.xml文件,注意名字和AppTheme相同:


[html] view plain copy

  1. <resources>  

  2.   

  3.     <style name="AppTheme" parent="android:Theme.Holo.NoActionBar.TranslucentDecor">  

  4.         <item name="android:windowTranslucentStatus">true</item>  

  5.         <item name="android:windowTranslucentNavigation">true</item>  

  6.     </style>  

  7.   

  8. </resources>  




(3)寫一個簡單的MainActivity.Java測試,MainActivity.Java代碼(特別注意!本例的MainActivity繼承自Activity而不是AppCompatActivity,如果繼承自AppCompatActivity,顯示結果達不到本例結果):


[java] view plain copy

  1. package zhangphil.myapplication;  

  2.   

  3. import android.app.Activity;  

  4. import android.os.Bundle;  

  5.   

  6. public class MainActivity extends Activity {  

  7.   

  8.     @Override  

  9.     protected void onCreate(Bundle savedInstanceState) {  

  10.         super.onCreate(savedInstanceState);  

  11.         setContentView(R.layout.activity_main);  

  12.     }  

  13. }  



MainActivity.java加載的activity_main.xml代碼:


[html] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     android:layout_width="match_parent"  

  4.     android:layout_height="match_parent"  

  5.     android:background="@android:color/holo_orange_dark"  

  6.     android:orientation="vertical">  

  7.   

  8.     <TextView  

  9.         android:layout_width="wrap_content"  

  10.         android:layout_height="wrap_content"  

  11.         android:layout_gravity="center"  

  12.         android:background="@android:color/white"  

  13.         android:text="zhang phil @ csdn" />  

  14. </LinearLayout>  




代碼運行結果:

TextView跑到頂部狀態欄下面去了,這顯然不合適,在activity_main.xml代碼中增加android:fitsSystemWindows="true" :


[html] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     android:layout_width="match_parent"  

  4.     android:layout_height="match_parent"  

  5.     android:background="@android:color/holo_orange_dark"  

  6.     android:fitsSystemWindows="true"  

  7.     android:orientation="vertical">  

  8.   

  9.     <TextView  

  10.         android:layout_width="wrap_content"  

  11.         android:layout_height="wrap_content"  

  12.         android:layout_gravity="center"  

  13.         android:background="@android:color/white"  

  14.         android:text="zhang phil @ csdn" />  

  15. </LinearLayout>  



代碼運行結果:


顯示就正常了。

二:使用代碼實現狀態欄透明

附錄1以xml寫style實現了Android沉浸式(侵入式)狀態欄(標題欄),同樣以上層Java代碼實現。
在附錄文章1的基礎上,本例僅僅只是刪掉res目錄下的全部values-v21目錄所有資源文件,僅保留values下一個styles.xml文件定義的AppTheme:


[html] view plain copy

  1. <resources>  

  2.   

  3.     <style name="AppTheme" parent="android:Theme.Holo.Light.NoActionBar">  

  4.   

  5.     </style>  

  6.   

  7. </resources>  



但是和附錄文章1比較,在MainActivity增加Java代碼:


[java] view plain copy

  1. package zhangphil.myapplication;  

  2.   

  3. import android.app.Activity;  

  4. import android.os.Build;  

  5. import android.os.Bundle;  

  6. import android.view.Window;  

  7. import android.view.WindowManager;  

  8.   

  9. public class MainActivity extends Activity {  

  10.   

  11.     @Override  

  12.     protected void onCreate(Bundle savedInstanceState) {  

  13.         super.onCreate(savedInstanceState);  

  14.         setContentView(R.layout.activity_main);  

  15.   

  16.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {  

  17.             Window window = getWindow();  

  18.             // Translucent status bar  

  19.             window.setFlags(  

  20.                     WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,  

  21.                     WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  

  22.   

  23.             // Translucent navigation bar  

  24.             window.setFlags(  

  25.                     WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,  

  26.                     WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);  

  27.         }  

  28.     }  

  29. }  




MainActivity需要的activity_main.xml和附錄文章1相同:


[html] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     android:layout_width="match_parent"  

  4.     android:layout_height="match_parent"  

  5.     android:background="@android:color/holo_orange_dark"  

  6.     android:fitsSystemWindows="true"  

  7.     android:orientation="vertical">  

  8.   

  9.     <TextView  

  10.         android:layout_width="wrap_content"  

  11.         android:layout_height="wrap_content"  

  12.         android:layout_gravity="center"  

  13.         android:background="@android:color/white"  

  14.         android:text="zhang phil @ csdn" />  

  15. </LinearLayout>  




代碼運行結果:


三,實現全部透明,在5.0以上會出現有底色覆蓋

從附錄文章1,2可以看到,依靠Android系統提供的標準方案,狀態欄即便在透明狀態下,仍然有些半透明而不是全透明。本文是Android沉浸式狀態欄解決方案的第三種,以Java代碼實現,參考附錄文章2,本文保持附錄文章2的styles.xml不變,僅僅只做上層Java代碼的調整,實現沉浸式狀態欄全透明或者動態設置顏色,測試的MainActivity.java:


[java] view plain copy

  1. package zhangphil.myapplication;  

  2.   

  3. import android.app.Activity;  

  4. import android.graphics.Color;  

  5. import android.os.Build;  

  6. import android.os.Bundle;  

  7. import android.view.View;  

  8. import android.view.Window;  

  9. import android.view.WindowManager;  

  10.   

  11. public class MainActivity extends Activity {  

  12.   

  13.     @Override  

  14.     protected void onCreate(Bundle savedInstanceState) {  

  15.         super.onCreate(savedInstanceState);  

  16.   

  17.         //核心代碼.  

  18.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  

  19.             Window window = getWindow();  

  20.             window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);  

  21.             window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);  

  22.             window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);  

  23.   

  24.             //給狀態欄設置顏色。我設置透明。  

  25.             window.setStatusBarColor(Color.TRANSPARENT);  

  26.             window.setNavigationBarColor(Color.TRANSPARENT);  

  27.         }  

  28.   

  29.         setContentView(R.layout.activity_main);  

  30.     }  

  31. }  




MainActivity.java需要的activity_main.xml文件:


[html] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     android:layout_width="match_parent"  

  4.     android:layout_height="match_parent"  

  5.     android:background="#e91e63"  

  6.     android:clipToPadding="false"  

  7.     android:fitsSystemWindows="true"  

  8.     android:orientation="vertical"  

  9.     android:paddingTop="50dp">  

  10.   

  11.     <TextView  

  12.         android:layout_width="wrap_content"  

  13.         android:layout_height="wrap_content"  

  14.         android:layout_gravity="center"  

  15.         android:background="@android:color/white"  

  16.         android:text="zhang phil @ csdn" />  

  17. </LinearLayout>  




代碼運行結果:



注意:

1,本文的解決方案不在受限於MainActivity繼承自標準Activity,可以繼承自AppCompatActivity,但是如果MainActivity繼承自AppCompatActivity,那麼必須把styles.xml文件的style修改爲Theme.AppCompat.*****,否則代碼將崩潰,比如可以修改styles中的AppTheme:

[html] view plain copy

  1. <resources>  

  2.   

  3.     <style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">  

  4.   

  5.     </style>  

  6.   

  7. </resources>  



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