Android沉浸式(侵入式)标题栏(状态栏)Status(三)
来源:互联网 发布:究极风暴4优化补丁1.5 编辑:程序博客网 时间:2024/06/06 01:43
Android沉浸式(侵入式)标题栏(状态栏)Status(三)
从附录文章1,2可以看到,依靠Android系统提供的标准方案,状态栏即便在透明状态下,仍然有些半透明而不是全透明。本文是Android沉浸式状态栏解决方案的第三种,以Java代码实现,参考附录文章2,本文保持附录文章2的styles.xml不变,仅仅只做上层Java代码的调整,实现沉浸式状态栏全透明或者动态设置颜色,测试的MainActivity.java:
- 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);
- }
- }
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文件:
- <?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>
<?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:
- <resources>
- <style name=“AppTheme” parent=“@style/Theme.AppCompat.Light.NoActionBar”>
- </style>
- </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
- Android沉浸式(侵入式)标题栏(状态栏)Status(三)
- Android沉浸式(侵入式)标题栏(状态栏)Status(三)
- Android沉浸式(侵入式)标题栏(状态栏)Status(一)
- Android沉浸式(侵入式)标题栏(状态栏)Status(二)
- Android沉浸式(侵入式)标题栏(状态栏)Status(一)
- Android沉浸式(侵入式)标题栏(状态栏)Status(二)
- android状态栏标题栏的沉浸式模式
- android沉浸式状态栏、fitsSystemWindows、标题栏折叠
- Android底部菜单栏、Android沉浸式状态栏(顶部状态栏修改颜色)、自定义标题栏
- Android 安卓 自定义标题栏+沉浸式状态栏
- Android UI 之自定义标题栏 + 沉浸式状态栏
- android沉浸式标题栏
- Android 沉浸式标题栏
- Android沉浸式状态栏(透明状态栏)
- Android 沉浸式状态栏 无标题栏 无状态栏 分层式状态栏
- Android 去掉状态栏 去掉标题栏 沉浸式状态栏 状态栏重叠 布局重叠 SystemBarTintManager systembartint-1.0.4
- Android 去掉状态栏 去掉标题栏 沉浸式状态栏 状态栏重叠 布局重叠 SystemBarTintManager systembartint-1.0.4
- 可折叠式标题栏+沉浸式状态栏
- 深入理解 Tomcat(五)源码剖析Tomcat 启动过程----类加载过程
- 深入理解 Tomcat(六)源码剖析Tomcat 启动过程----生命周期和容器组件
- A. Pizza Separation
- B
- 深入理解 Tomcat(七)源码剖析 Tomcat 完整启动过程
- Android沉浸式(侵入式)标题栏(状态栏)Status(三)
- SQL零起点(GETTING STARTED WITH SQL)翻译前言
- 深入理解 Tomcat(八)源码剖析之连接器
- 深入理解 Tomcat(九)源码剖析之请求过程
- 深入理解 Tomcat(十) 总结
- SQL Server 登录(local) 失败
- Egret使用心得
- POJ1990 (树状数组)
- DevOps中的软件架构与微服务