Android 4.4 沉浸式透明状态栏与导航栏

Android 4.4 沉浸式透明状态栏与导航栏,android4.4


热度1    评论 258 html

www.BkJia.Com   网友分享于:  2014-11-18 09:11:17     浏览数22745次

Android 4.4 沉浸式透明状态栏与导航栏,android4.4


Android 系统自4.2 开始 UI 上就没多大改变,4.4 也只是增长了透明状态栏与导航栏的功能,如图android

spacer.gif


那么如今我就来给你们讲解下如何使用这个新特性,让你的 app 跟随潮流,固然若是你不在意外观就算了,
使用这个特性能开发出很漂亮的UI,尤为对于 google 原生系统,屏幕下方的导航栏白白占据一块屏幕空间,看起来很不爽



OK废话很少讲,开始讲技术吧,第一种方法,在代码设置:
git

  1. if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
    github

  2.                                 //透明状态栏
    swift

  3.                                 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    app

  4.                                 //透明导航栏
    ide

  5.                                 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    性能

  6.                         }google

直接调用上面2行代码能够透明,可是你会发现你的 view 跑到 actionbar 上面去了,很明显 google 的意图是使你的 view 能够占据整个屏幕,而后 状态栏和导航栏 透明覆盖在上面很明显这样不可行。
那有没有办法使你的 view 保持原来大小呢?
有,你须要在这个 activity 的 layout xml 文件添加两个属性
url

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  2.     android:layout_width="fill_parent"

  3.     android:layout_height="fill_parent"

  4.     android:gravity="center_horizontal"

  5.     

  6.     android:fitsSystemWindows="true"

  7.     android:clipToPadding="true"

  8.     

  9.     android:orientation="vertical" >

这样状态栏的背景就是你的 activity 的主背景,假若actionbar 在,将会很难看,如图:

事实证实,google 并无提供一个比较好的解决方案,他的 透明状态栏与导航栏的应用局限于,全屏阅读文字或玩游戏那种情景,


第二种方式,是是设置 theme 属性

  1. android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar.TranslucentDecor"

  2.             android:theme="@android:style/Theme.Holo.Light.NoActionBar.TranslucentDecor"

  3.             android:theme="@android:style/Theme.Holo.NoActionBar.TranslucentDecor"

复制代码若是你使用自定主题,只需在在 values-19 文件添加如下属性:

  1. <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">


  2.         <!-- API 19 theme customizations can go here. -->

  3.         <item name="android:windowTranslucentStatus">true</item>

  4.         <item name="android:windowTranslucentNavigation">true</item>

  5.     </style>

复制代码
刚刚说了这个使用有局限性,不过好在有一个开源的东西
https://github.com/jgilfelt/SystemBarTint


使用这个开源库,必须开启透明标题栏

使用这个主题

  1. <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">


  2.         <!-- API 19 theme customizations can go here. -->

  3.         <item name="android:windowTranslucentStatus">true</item>

  4.         <item name="android:windowTranslucentNavigation">true</item>

  5.     </style>


或者在setContentView以前调用这个代码


  1. if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {

  2.                                 //透明状态栏

  3.                                 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

  4.                                 //透明导航栏

  5.                                 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

  6.                         }

相关文章
相关标签/搜索