Android沉浸式与SearchView的坑
转载注明出处:简书-十个雨点
Android开发虽然不算多难,但其中确实包含了这样那样的坑,我今天要说的这个坑可能很少人会遇到,不过万一呢。。。
一开始是我想在项目的toolbar上添加谷歌原生支持的搜索功能,但是怎么弄都不对,键盘弹出以后搜索框会自动消失,如下图所示,可能比较乱,将就看看吧:

经过两天的不断修改尝试,终于找到了症结所在:
在styles.xml文件中设置windowTranslucentStatus=true会导致SearchView显示不正确!
同时还得到一个结论:
targetSdkVersion对最终编译结果有较大的影响!
先说targetSdkVersion对最终编译结果的影响,这些影响主要在support库的界面上。比如同样使用Theme.AppCompat.Light.NoActionBar,targetSdkVersion<21编译的结果运行在api21以上的手机上,就不自带沉浸式状态栏。而targetSdkVersion>=21编译的就自带沉浸式状态栏,不过api19的还是需要使用windowTranslucentStatus来开启沉浸式状态栏的。
再比如跟上例一样的targetSdkVersion设置,用同样的NotificationCompat生成的通知的显示效果是不一样的,如下图:


甚至还有更可怕的,是会影响到一些奇怪的地方,比如友盟的反馈组件界面:


我知道一般人是看不出差别来的,那么看下面这个图:

这下看得出来了吧,完全影响到界面布局了,而且语音按钮的点击功能也失效了!!!
所以为了保证友盟的反馈组件的可用性,我只能现在targetSdkVersion=15了。
具体的原因还不太清楚,可能是我自己本身哪里使用得不太对吧。。。
再说回到windowTranslucentStatus。
为什么要设置windowTranslucentStatus呢?windowTranslucentStatus顾名思义是透明状态栏的意思,参照前面所说的,如果targetSdkVersion<21,如果要实现沉浸式的效果,就需要设置windowTranslucentStatus,或者如果想要api19也实现沉浸式效果,无论targetSdkVersion为多少都要设置windowTranslucentStatus。具体如何实现沉浸式状态栏,可以参考这篇。
那是不是沉浸式状态栏和SearchView就不能同时使用了呢?当然不是:
- 如果你的targetSdkVersion>=21,那么只要不给api19设置沉浸式效果就可以了,api21以上的版本会自动开启沉浸式效果,SearchView工作也正常。
- 如果你的targetSdkVersion<21,或者你想给api19也设置沉浸式效果,那么就将styles.xml里面的windowTranslucentStatus删掉,然后使用代码的方式来实现沉浸式吧。
下面是简单的沉浸式代码实现:
public class StatusBarCompat {
public static void setupStatusBarView(Activity activity, ViewGroup decorViewGroup, boolean on,int colorRes) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(activity, on);
View mStatusBarTintView = new View(activity);
int mStatusBarHeight = 0;
int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
mStatusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
}
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, mStatusBarHeight);
params.gravity = Gravity.TOP;
mStatusBarTintView.setLayoutParams(params);
mStatusBarTintView.setBackgroundResource(colorRes);
mStatusBarTintView.setVisibility(View.VISIBLE);
decorViewGroup.addView(mStatusBarTintView);
}
}
private static void setTranslucentStatus(Activity activity,boolean on) {
Window win = activity.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);
}
}
其实这个WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS的作用和windowTranslucentStatus是一样的,但是为什么通过代码的方式和通过资源文件的方式来实现有这么大的差别就不清楚了。