安卓透明状态栏小技巧

随着安卓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>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值