zoukankan      html  css  js  c++  java
  • 兼容 Android 4.4 透明状态栏与导航栏

    http://www.apkbus.com/Android-163388-1-1.html?_dsign=73d41229

    android 系统自4.2 开始 UI 上就没多大改变,4.4 也只是增加了透明状态栏与导航栏的功能,如图
    左边为 4.2.2 右边为 4.4.2
            


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



    图为 sommth 客户端,

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

    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.                         }
    复制代码

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

    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
    可以设置 statusbar 背景,原理是在 Window 的 DocView 添加 view,大家可以下载这个项目学习如何使用
     SystemBarTint-master.zip (513.74 KB, 下载次数: 1995) 

  • 相关阅读:
    在SharePoint 2010中,如何找回丢失的服务账号(Service Account)密码
    基于Picture Library创建的图片文档库中的上传多个文件功能(upload multiple files)报错怎么解决?
    使用PowerShell找出具体某个站点所使用的模板(Web Template)名称?
    多文档上传(upload multiple documents)功能不能使用怎么办?
    实验环境里新创建成功的web application却在浏览器中返回404错误
    SharePoint 2010中一些必须知道的限制
    Information Management Policy(信息管理策略)的使用范例
    Google云平台对于2014世界杯半决赛的预测,德国阿根廷胜!
    php 字符串分割输出
    php实现验证码(数字、字母、汉字)
  • 原文地址:https://www.cnblogs.com/dhcn/p/7121296.html
Copyright © 2011-2022 走看看