Android 4.4 沉浸式透明状态栏与导航栏
2015-02-09 17:19
387 查看
Android 系统自4.2 開始 UI 上就没多大改变,4.4 也仅仅是添加了透明状态栏与导航栏的功能,如图
![](http://www.cnblogs.com/mfrbuaa/p/file:/c:/users/tan/appdata/local/360chrome/chrome/User Data/temp/154714jxaom0117a66o81t.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/19/daab2bc56f5811396684dc98bff688de.png)
那么如今我就来给大家解说下怎样使用这个新特性,让你的 app 尾随潮流,当然假设你不在乎外观就算了,
使用这个特性能开发出非常美丽的UI,尤其对于 google 原生系统,屏幕下方的导航栏白白占领一块屏幕空间,看起来非常不爽
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/19/82a9a21db8815f8cf22f933fcd7eb741.png)
OK废话不多讲,開始讲技术吧,第一种方法,在代码设置:
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 在,将会非常难看,如图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/19/5113679b6b2a1d556d6e99b7da159f46.png)
事实证明,google 并没有提供一个比較好的解决方式,他的 透明状态栏与导航栏的应用局限于,全屏阅读文字或玩游戏那种情景,
另外一种方式,是是设置 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);
}
![](http://www.cnblogs.com/mfrbuaa/p/file:/c:/users/tan/appdata/local/360chrome/chrome/User Data/temp/154714jxaom0117a66o81t.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/19/daab2bc56f5811396684dc98bff688de.png)
那么如今我就来给大家解说下怎样使用这个新特性,让你的 app 尾随潮流,当然假设你不在乎外观就算了,
使用这个特性能开发出非常美丽的UI,尤其对于 google 原生系统,屏幕下方的导航栏白白占领一块屏幕空间,看起来非常不爽
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/19/82a9a21db8815f8cf22f933fcd7eb741.png)
OK废话不多讲,開始讲技术吧,第一种方法,在代码设置:
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 在,将会非常难看,如图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/19/5113679b6b2a1d556d6e99b7da159f46.png)
事实证明,google 并没有提供一个比較好的解决方式,他的 透明状态栏与导航栏的应用局限于,全屏阅读文字或玩游戏那种情景,
另外一种方式,是是设置 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 沉浸式透明状态栏与导航栏
- Android 4.4 沉浸式透明状态栏与导航栏
- Android 4.4 沉浸式透明状态栏与导航栏,android4.4
- Android 4.4 沉浸式透明状态栏与导航栏,android4.4
- Android 4.4 沉浸式透明状态栏与导航栏
- Android 4.4 沉浸式透明状态栏与导航栏
- androidの4.4版本沉浸式透明状态栏与导航栏案例
- Android中沉浸式透明状态栏和导航栏的终极解决方案
- 兼容 Android 4.4 透明状态栏与导航栏
- 兼容 Android 4.4 透明状态栏与导航栏
- 兼容 Android 4.4 透明状态栏与导航栏
- Android 4.4 后透明状态栏和导航栏效果实现(学习总结)
- 对系统版本为4.4及以上的开启 透明状态栏和导航栏(即沉浸式状态栏)
- 兼容 Android 4.4 透明状态栏与导航栏
- 兼容 Android 4.4 透明状态栏与导航栏
- 兼容 Android 4.4 透明状态栏与导航栏
- Android 沉浸式透明状态栏与导航栏
- 【Android】4.4以上使用透明状态栏后布局被软键盘遮挡的解决办法
- Android 安卓 4.4 沉浸式状态栏,导航栏
- Android 4.4 以上实现透明导航栏和状态栏 Translucent system bar