android KitKat Translucent 4.4版本支持消息栏半透明
2015-12-15 17:13
781 查看
Android 从 4.4(KitKat) 开始提供了一个视觉上的提升,让位在装置最上方的状态列 (Status Bar) 以及最下方的导航列 (Navigation Bar) 可以被透明化,并让 APP 的内容可以往上下延伸,使整个画面的可被利用度大幅提升
从 3.0
(honeycomb) 开始,导航列被虚拟化,一直都会占住装置一块不算小的空间,对很多人来说,整个画面无法充份利用,是一件相当痛苦的事情。也因此,有些人就会刻意去挑选仍维持著实体键设计的装置。
而 Google 似乎也意识到这个状况,从 4.4 (KitKat) 提供了开发者一个新的作法,让我们可以把导航列 (Navigation Bar)给透明化,并让内容延伸到该处,甚至是状态列 (Status Bar) 也可以被设定透明,这样再搭配 Action Bar
的配色,可以像上图一般,让整个 APP 更显得一致。
if
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
// Translucent status bar
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// Translucent navigation bar
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
的确,程式码就是这麼短,一行设定状态列 (Status Bar)、一行设定导航列 (Navigation Bar) 。
别忘了用个判断式,确定装置的版本是 4.4 (KitKat) 以上来进行容错。
再来,有个部份要稍微留意一下,如果不希望
APP 的内容被上拉到状态列 (Status bar) 的话,要记得在介面 (Layout) XML 档中,最外面的那层,要再加上一个属性 fitsSystemWindows 为 true,以这边的例子来说,请见下方
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity" >
<!-- Content -->
</RelativeLayout>
在介面的根层加入 android:fitsSystemWindows=”true” 这个属性,这样就可以让内容介面从 Action Bar 下方开始。
再来,若我们的 APP 可以从 4.4 (KitKat) 开始支援,那其实可以直接从 style 去进行设定,我们可以在官网上看到对透明化的说明里,官方提供了两种 no title 的主题风格可以让我们使用,分别如下
Theme.Holo.Light.NoActionBar.TranslucentDecor
这样我们就可以做出全萤幕的
APP。
若是我们有自己设定好的主题,或说希望可以维持原有 Action Bar 存在,那只要在主题设定中分别加入两个属性值即可
<style
name="AppTheme" parent="AppBaseTheme">
<!-- Status Bar -->
<item name="android:windowTranslucentStatus">true</item>
<!-- Navigation Bar -->
<item name="android:windowTranslucentNavigation">true</item>
</style>
跟程式码一样,也是两行完成,上面一行是设定状态列 (Status Bar)、下面一行是设定导航列 (Navigation Bar) 。别忘了,如果不希望内容被 Action Bar 压住,那先前提及的 Layout 属性 android:fitsSystemWindows=”true”
要设定到。
其实以现在的状况来说,透过程式码去设定是最为安全的,毕竟目前绝大部份的装置都还未被升级到 4.4 (KitKat)。
从 3.0
(honeycomb) 开始,导航列被虚拟化,一直都会占住装置一块不算小的空间,对很多人来说,整个画面无法充份利用,是一件相当痛苦的事情。也因此,有些人就会刻意去挑选仍维持著实体键设计的装置。
而 Google 似乎也意识到这个状况,从 4.4 (KitKat) 提供了开发者一个新的作法,让我们可以把导航列 (Navigation Bar)给透明化,并让内容延伸到该处,甚至是状态列 (Status Bar) 也可以被设定透明,这样再搭配 Action Bar
的配色,可以像上图一般,让整个 APP 更显得一致。
if
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
// Translucent status bar
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// Translucent navigation bar
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
的确,程式码就是这麼短,一行设定状态列 (Status Bar)、一行设定导航列 (Navigation Bar) 。
别忘了用个判断式,确定装置的版本是 4.4 (KitKat) 以上来进行容错。
再来,有个部份要稍微留意一下,如果不希望
APP 的内容被上拉到状态列 (Status bar) 的话,要记得在介面 (Layout) XML 档中,最外面的那层,要再加上一个属性 fitsSystemWindows 为 true,以这边的例子来说,请见下方
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity" >
<!-- Content -->
</RelativeLayout>
在介面的根层加入 android:fitsSystemWindows=”true” 这个属性,这样就可以让内容介面从 Action Bar 下方开始。
再来,若我们的 APP 可以从 4.4 (KitKat) 开始支援,那其实可以直接从 style 去进行设定,我们可以在官网上看到对透明化的说明里,官方提供了两种 no title 的主题风格可以让我们使用,分别如下
Theme.Holo.Light.NoActionBar.TranslucentDecor
这样我们就可以做出全萤幕的
APP。
若是我们有自己设定好的主题,或说希望可以维持原有 Action Bar 存在,那只要在主题设定中分别加入两个属性值即可
<style
name="AppTheme" parent="AppBaseTheme">
<!-- Status Bar -->
<item name="android:windowTranslucentStatus">true</item>
<!-- Navigation Bar -->
<item name="android:windowTranslucentNavigation">true</item>
</style>
跟程式码一样,也是两行完成,上面一行是设定状态列 (Status Bar)、下面一行是设定导航列 (Navigation Bar) 。别忘了,如果不希望内容被 Action Bar 压住,那先前提及的 Layout 属性 android:fitsSystemWindows=”true”
要设定到。
其实以现在的状况来说,透过程式码去设定是最为安全的,毕竟目前绝大部份的装置都还未被升级到 4.4 (KitKat)。
相关文章推荐
- Android成长之路-LayoutInflater和inflate的用法
- Android_Bitmap压缩杂谈
- Android多媒体应用——图片资源获取的5种姿势
- ubuntu14.04 编译 vlc-android
- Android自定义控件使用到XML的时候报错no such method
- android 百度地图定位与覆盖物的添加,以及他们的点击事件
- Android开发艺术2之Activity的启动模式
- Android字符串资源及其格式化
- Android去掉标题栏
- Android EditText在开始显示时失去焦点
- Android 2D Graphics学习(二)、Canvas篇2、Canvas裁剪和Region、RegionIterator
- Android自定义Switch开关
- 基于 Android NDK 的学习之旅-----Android.mk 介绍
- Activity跳转生命周期变化
- Android NDK开发之HelloWorld
- Android之ListView分页获取网路数据(服务器端)(一)
- conn.getInputStream 显示为空指针
- Android沉浸式状态栏(二)
- Uncaught ReferenceError while loading asset file on android 4.4
- android之遍历所有文件夹和子目录搜索文件