Android沉浸式(侵入式)标题栏(状态栏)Status(三)

来源:互联网 发布:究极风暴4优化补丁1.5 编辑:程序博客网 时间:2024/06/06 01:43


Android沉浸式(侵入式)标题栏(状态栏)Status(三)

从附录文章1,2可以看到,依靠Android系统提供的标准方案,状态栏即便在透明状态下,仍然有些半透明而不是全透明。本文是Android沉浸式状态栏解决方案的第三种,以Java代码实现,参考附录文章2,本文保持附录文章2的styles.xml不变,仅仅只做上层Java代码的调整,实现沉浸式状态栏全透明或者动态设置颜色,测试的MainActivity.java:

[java] view plain copy
print?
  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. }  
package zhangphil.myapplication;import android.app.Activity;import android.graphics.Color;import android.os.Build;import android.os.Bundle;import android.view.View;import android.view.Window;import android.view.WindowManager;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //核心代码.        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            Window window = getWindow();            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);            //给状态栏设置颜色。我设置透明。            window.setStatusBarColor(Color.TRANSPARENT);            window.setNavigationBarColor(Color.TRANSPARENT);        }        setContentView(R.layout.activity_main);    }}


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

[html] view plain copy
print?
  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>  
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#e91e63"    android:clipToPadding="false"    android:fitsSystemWindows="true"    android:orientation="vertical"    android:paddingTop="50dp">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:background="@android:color/white"        android:text="zhang phil @ csdn" /></LinearLayout>


代码运行结果:



注意:

1,本文的解决方案不在受限于MainActivity继承自标准Activity,可以继承自AppCompatActivity,但是如果MainActivity继承自AppCompatActivity,那么必须把styles.xml文件的style修改为Theme.AppCompat.*****,否则代码将崩溃,比如可以修改styles中的AppTheme:

[html] view plain copy
print?
  1. <resources>  
  2.   
  3.     <style name=“AppTheme” parent=“@style/Theme.AppCompat.Light.NoActionBar”>  
  4.   
  5.     </style>  
  6.   
  7. </resources>  
<resources>    <style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">    </style></resources>


2,本文提供解决方案MainActivity中的那部分核心Java代码显然支持到Android 5.0即API 21或以上。



附录:
1,《Android沉浸式(侵入式)标题栏(状态栏)Status(一)》链接:http://blog.csdn.net/zhangphil/article/details/52622758 
2,《Android沉浸式(侵入式)标题栏(状态栏)Status(二)》链接:http://blog.csdn.net/zhangphil/article/details/52622971
3,《Android StatusBarUtil:设置Android系统下方虚拟键键盘透明度》链接:http://blog.csdn.net/zhangphil/article/details/51768318

转自:http://blog.csdn.net/zhangphil/article/details/52624191

阅读全文
0 0
原创粉丝点击