闲云笔记——Android
2016-04-13 17:39
375 查看
1. 沉浸式状态栏
1.1 使用开源的SystemBarTint,很方便对状态栏、导航栏进行设置:
tintManager = new SystemBarTintManager(activity); // 激活状态栏设置 tintManager.setStatusBarTintEnabled(true);
然后就可以使用了,如设置状态栏颜色为自己titlebar的颜色:
tintManager.setStatusBarTintColor(title_color);
1.2 坑。使用以上开源代码管理状态栏很方便,但是也是有不足之处的:
目前我发现的问题是:一个信息录入页面使用ScrollView包含多个EditText,这样在软键盘呼出状态下,页面仍然是可滚动的,提升用户体验,但是,使用SystemBarTintManager管理状态栏时,软键盘呼出状态下页面变得不可滚动,所以变成了好看的状态栏和较好的用户输入体验只能二选一。1.3 原因
跟踪了下SystemBarTintManager源码,发现它处理状态栏的方式是把状态栏设置透明,在页面头部添加一个高度为状态栏高度的view,没有对页面的fitsSystemWindows属性进行设置,所以是false。而我发现将状态栏设置为透明以后,如果不把页面的根布局(还必须得是根布局,否则本文所提需求仍然无法实现)属性fitsSystemWindows设置为true,在软键盘弹出状态(无论什么键盘模式)时页面ScrollView就失效了。这应该是Android系统的一个bug吧。看一下fitsSystemWindows作用:让view可以根据系统窗口(如status bar)来调整自己的布局,如果值为true,就会调整view的paingding属性来给system windows留出空间。当status bar为透明或半透明时(4.4以上),系统会设置view的paddingTop值为一个适合的值(status bar的高度)让view的内容不被上拉到状态栏,当在不占据status bar的情况下(4.4以下)会设置paddingTop值为0(因为没有占据status bar所以不用留出空间)。
所以在本文所提需求下,就需要设置fitsSystemWindows作为前提,但是又不能简单地在使用SystemBarTint的页面设置该属性为true,因为那样在4.4以上系统运行的应用顶部必然会出现2个状态栏高度。
当然,如果不要求在软键盘弹出情况下页面ScrollView仍然有效,那么使用SystemBarTint的同时使用ScrollView效果是正常的。
1.4 曲线救国方法:
1.4.1 在页面的根布局添加属性:
android:fitsSystemWindows = "true" android:background="@color/title_color"
不过,键盘开始弹出到弹出完成期间,因为会使用页面背景作为那个区域的填充,所以这里自定义一个头部为titlebar颜色,下部为页面背景颜色的xml作为背景有助于提升视觉体验,上面的background换成:
android:background="@drawable/inputpage_blue_status"
inputpage_blue_status.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item > <layer-list > <item > <shape android:shape="rectangle"> <stroke android:width="40dp" android:color="@color/title_color"/> </shape> </item> <item android:top="40dp" > <shape android:shape="rectangle" > <solid android:color="@color/main_bg_lightgray"/> </shape> </item> </layer-list> </item> </selector>
1.4.2 设置软键盘模式:
在AndroidManifast中为该页面指定android:windowSoftInputMode=”adjustResize” 或者代码中getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);1.4.3 继续配置页面样式:
// 设置窗口无标题 requestWindowFeature(Window.FEATURE_NO_TITLE); // 4.4以上系统,则设置状态栏透明 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = getWindow(); window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); }
至此,在需要较好输入体验的页面也能同时有沉浸式的状态栏了,不过这肯定不是最好的方法,有时间再研究,可以试试修改下SystemBarTint代码。
相关文章推荐
- android之Notification实现
- Android应用锁之获取栈顶Activity
- android通用JSON解析
- 如何关闭android的一个service
- cordova开发插件步骤及小结(android插件)
- android.util.log(Log.v()Log.d()Log.i()Log.w()和Log.e())
- Pro Android学习笔记(一五四):传感器(4):陀螺仪、加速传感器
- Android蓝牙BLE低功耗相关简单总结
- android中ListView控件&&onItemClick事件中获取listView传递的数据
- Android程序的安全系统 apk获得root权限
- Android动画之旅一Android动画基本介绍
- Android mediaPlayer属性详解
- Android 5.0以后版本打开“有权查看使用情况的应用”
- android xml之Drawable 篇 --------shape和selector和layer-list的
- Android开发笔记(八十五)手机数据库Realm
- android 5.0以上获取前台应用的包名
- 初试Android的MVP框架
- Android群英传--绘图机制和处理技巧(二)
- Android - Fragments
- Android APK反编译就这么简单 详解(附图)