Android中怎么实现一个沉浸式状态栏功能

发布时间:2021-08-07 15:19:24 作者:Leah
来源:亿速云 阅读:133

Android中怎么实现一个沉浸式状态栏功能,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

具体内容如下

1.实现沉浸式状态栏有两种方式,一种是通过写Theme主题的方式,另一种是写代码的方式。

2. 先来介绍写主题的方式

2.1 先在res包下新建values-v19和values-v21两个包,为了兼容Android高低版本

2.2 然后分别在包中新建styles.xml文件

2.2.1 values-v19包中styles.xml文件中的内容为:

<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 values-v21包中styles.xml文件中的内容为:

<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 在values包中的styles.xml文件中添加一个空的,起到后备作用

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

2.2.4 最后一点需要在对应的布局文件中添加,然后在AndroidManifest.xml引用

android:fitsSystemWindows="true"

写主题的方式就算完成了

3.再来介绍一下写代码的方式

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隐藏 }

看完上述内容,你们掌握Android中怎么实现一个沉浸式状态栏功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. Android实现沉浸式状态栏功能
  2. Android中如何实现一个沉浸式状态栏

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android

上一篇:Android中怎么实现清除应用缓存功能

下一篇:如何解决某些HTML字符打不出来的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》