android状态栏沉浸效果(还没整理好,晚上整理)
2015-05-15 14:27
204 查看
一.修改样式文件:
1.增加values-v19文件夹,再其中的styles.xml中加上:
二.使用开源库
1.android studio中加入依赖:
2.在activity的onCreate()中调用下面方法:
使用以上两个方法的最后,都要在主布局中加上:
否则标题栏是白色的。
if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_N***IGATION);
}
直接调用上面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 在,将会很难看,如图:
事实证明,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_N***IGATION);
1.增加values-v19文件夹,再其中的styles.xml中加上:
二.使用开源库
1.android studio中加入依赖:
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
2.在activity的onCreate()中调用下面方法:
/** * Apply KitKat specific translucency. */ private void applyKitKatTranslucency() { // KitKat translucent navigation/status bar. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { setTranslucentStatus(true); SystemBarTintManager mTintManager = new SystemBarTintManager(this); mTintManager.setStatusBarTintEnabled(true); mTintManager.setNavigationBarTintEnabled(true); // mTintManager.setTintColor(0xF00099CC); mTintManager.setTintDrawable(UIElementsHelper .getGeneralActionBarBackground(this)); } } @TargetApi(19) private void setTranslucentStatus(boolean on) { Window win = getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; if (on) { winParams.flags |= bits; } else { winParams.flags &= ~bits; } win.setAttributes(winParams); }
使用以上两个方法的最后,都要在主布局中加上:
android:fitsSystemWindows="true"
否则标题栏是白色的。
if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_N***IGATION);
}
直接调用上面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 在,将会很难看,如图:
事实证明,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_N***IGATION);
相关文章推荐
- Android学习整理 - 状态栏和虚拟导航键透明效果
- android使状态栏沉浸效果
- Android 状态栏背景模糊透明效果实现
- Android上透明状态栏,全屏显示以及沉浸模式
- Android 6.0 以上实现状态栏白底黑字 「白色标题栏 App 享受 iOS 般的沉浸体验」
- android沉浸状态栏和顶部状态栏背景色的设置
- Android 沉浸式状态栏及悬浮效果
- Android之---沉浸式状态栏的错误理解(官方有沉浸模式的感念但是没有沉浸式状态栏的说法)
- Android隐藏状态栏和标题栏,相当于全屏效果
- 玩转Android上透明状态栏,全屏显示以及沉浸模式(Immersive Mode)
- Android 沉浸状态栏
- Android中的沉浸式状态栏效果
- Android沉浸(透明状态栏)
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果[整理后的源码]
- android 状态栏沉浸
- Android开发 状态栏沉浸
- 关于Android UI效果编程与思考 - 整理中
- Android内容覆盖透明状态栏下实现全屏模式下带状态栏的效果
- Android如何一步步实现状态栏一体化效果
- android沉浸(透明状态栏)