Android implements immersive status bar function

Android implements the function of immersive status bar for your reference. The specific contents are as follows

1. There are two ways to implement the immersive status bar: one is to write theme, and the other is to write code. If you want to make multiple pages appear in the immersive status bar, it is more convenient to use the theme. If you only want to make a single page appear, it is better to use the code! Of course, it depends on your personal preferences.

2. Let's first introduce the way to write the topic

2.1 create values-v19 and values-v21 packages under res package to be compatible with Android versions

2.2 then create a new styles.xml file in the package

2.2.1 the contents of styles.xml file in values-v19 package are:

 <style name="AppTheme.TransparentStausBar" parent="Theme.AppCompat.Light.DarkActionBar">
  <item name="windowActionBar">false</item>  //取消系统认的actionBar
  <item name="windowNoTitle">true</item>  //取消actionBar的标题
  <item name="android:windowTranslucentStatus">true</item> //允许页面可以拉伸到顶部状态栏并且定义顶部状态栏透明,安卓4.4才有
  <item name="android:windowTranslucentNavigation">true</item>//设置虚拟键透明
 </style>

2.2.2 the contents in the styles.xml file in the values-v21 package are:

 <style name="AppTheme.TransparentStausBar" parent="Theme.AppCompat.Light.DarkActionBar">
  <item name="windowActionBar">false</item>  //取消系统认的actionBar
  <item name="windowNoTitle">true</item>   //取消actionBar的标题
  <item name="android:windowTranslucentStatus">false</item> //允许页面可以拉伸到顶部状态栏并且定义顶部状态栏透明,安卓4.4才有
  <item name="android:windowTranslucentNavigation">true</item> //设置虚拟键透明
  <item name="android:statusBarColor">@android:color/transparent</item> //设置状态栏的颜色为透明
</style>

2.2.3 add an empty in the styles.xml file in the values package to play a backup role

<style name="AppTheme.TransparentStausBar" parent="AppTheme">

</style>

2.2.4 the last point needs to be added in the corresponding layout file, and then referenced in androidmanifest.xml

android:fitsSystemWindows="true"

The way to write the topic is finished

3. Let's introduce the way of writing code

private void initBar() {
 getWindow().requestFeature(Window.FEATURE_NO_TITLE); //取消状态栏的标题
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//判断SDK的版本是否>=21
   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); //设置虚拟键为透明
  }
  ActionBar actionBar = getSupportActionBar();
  actionBar.hide();   //将actionBar隐藏
 }

The way of writing code is also completed

Tip: Xiaobai, sorry for your poor writing. If there is anything wrong, please leave a message.

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>