首页 > 移动平台 > 详细

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

时间:2017-01-24 13:32:11      阅读:266      评论:0      收藏:0      [点我收藏+]

安卓4.4才有的沉浸式状态栏

在代码设置:

if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
    //透明状态栏
   getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
   //透明导航栏
   getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}

直接调用上面2行代码可以透明,但是你会发现你的 view 跑到 actionbar 上面去了,很明显

google 的意图是使你的 view 可以占据整个屏幕,然后 状态栏和导航栏 透明覆盖在上面很明显这样不可行。
那有没有办法使你的 view 保持原来大小呢?
有,你需要在这个 activity 的 layout xml 文件添加两个属性:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    
    android:fitsSystemWindows="true"
    android:clipToPadding="true"
    
    android:orientation="vertical" >

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

技术分享

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

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

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

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- API 19 theme customizations can go here. -->
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
</style>

复制代码 

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


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

 

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- API 19 theme customizations can go here. -->
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
</style>

使用这个主题或者在setContentView之前调用这个代码

 

if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
     //透明状态栏
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
     //透明导航栏
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}

 

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

原文:http://www.cnblogs.com/Oldz/p/6346858.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!