Android沉浸式(侵入式)标题栏(状态栏)Status(三)
2017-11-29 00:20
447 查看
Android沉浸式(侵入式)标题栏(状态栏)Status(三)从附录文章1,2可以看到,依靠Android系统提供的标准方案,状态栏即便在透明状态下,仍然有些半透明而不是全透明。本文是Android沉浸式状态栏解决方案的第三种,以Java代码实现,参考附录文章2,本文保持附录文章2的styles.xml不变,仅仅只做上层Java代码的调整,实现沉浸式状态栏全透明或者动态设置颜色,测试的MainActivity.java:
[java] view plain copy print?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?<?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>
代码运行结果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201711/b18bd229449103ac2c51a14cb662743f)
注意:
1,本文的解决方案不在受限于MainActivity继承自标准Activity,可以继承自AppCompatActivity,但是如果MainActivity继承自AppCompatActivity,那么必须把styles.xml文件的style修改为Theme.AppCompat.*****,否则代码将崩溃,比如可以修改styles中的AppTheme:
[html] view plain copy print?<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
[java] view plain copy print?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文件:
[html] view plain copy print?<?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:
[html] view plain copy print?<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沉浸式(侵入式)标题栏(状态栏)Status(一)
- Android沉浸式(侵入式)标题栏(状态栏)Status(二)
- Android沉浸式(侵入式)标题栏(状态栏)Status(二)
- Android沉浸式(侵入式)标题栏(状态栏)Status(三)
- Android沉浸式(侵入式)标题栏(状态栏)Status(三)
- TitleLayout——一个Android轻松实现通用、标准、支持沉浸式状态栏的标题栏库
- android沉浸式状态栏、fitsSystemWindows、标题栏折叠
- TitleLayout——一个Android轻松实现通用、标准、支持沉浸式状态栏的标题栏库
- Android 去掉状态栏 去掉标题栏 沉浸式状态栏 状态栏重叠 布局重叠 SystemBarTintManager systembartint-1.0.4
- Android UI 之自定义标题栏 + 沉浸式状态栏
- android界面之沉浸式全屏等状态栏标题栏全解析
- Android沉浸式标题栏状态栏字体颜色修改(小米和魅族以及Android6.0)
- Android 安卓 自定义标题栏+沉浸式状态栏
- Android底部菜单栏、Android沉浸式状态栏(顶部状态栏修改颜色)、自定义标题栏