Android为ToolBar设置沉浸式状态栏及其相关问题处理
2017-11-28 本文已影响794人
WonderSky_HY
这里推荐一下鸿洋大神写的一篇博客Android 沉浸式状态栏攻略 让你的状态栏变色吧,文章中写的很详细,可以作为参考,因此这里只是简单讲述下设置沉浸式状态栏的步骤,主要是说一下我在使用过程中碰到的一些问题以及解决方法。
设置沉浸式状态栏
- 如果看过上面推荐的博客就会知道,设置沉浸式状态栏通常分为3个步骤(缺一不可)
- 在styles文件中添加
<item name="android:windowTranslucentStatus">true</item>
, - Toolbar布局文件中设置高度为
android:layout_height="wrap_content"
, - Toolbar布局添加
android:fitsSystemWindows="true"
属性(注意:如果ToolBar外层还包裹了AppBarLayout布局,则需要将android:fitsSystemWindows="true"
属性设置在AppBarLayout布局中。另外如果设置了侧滑菜单,要将菜单布局也延伸到状态栏中的话,同样需要在菜单布局文件的根布局中设置android:fitsSystemWindows属性)。
遇到的问题
-
界面中有EditText组件,当EditText获取焦点弹出软键盘时,ToolBar的布局会延伸,如下图所示:
image.png
-
当有登录界面并且登录界面输入框较多弹出的软键盘将下面的输入框挡住时,即使将这些输入框用ScrollView包裹并且在manifest.xml文件中设置了
android:windowSoftInputMode="adjustResize|stateHidden"
时,会有一部分布局依然是被软键盘挡住,无法滑动上来)。 -
在开发中我们通常会遇到实现一些信息录入的功能,界面布局通常也是最上面是ToolBar,最下面是一个保存按钮(有时也会添加一个取消按钮,也有可能是一个Tab组件),中间则是多个信息输入框,一般情况下我是比较希望当输入框获取焦点弹出软键盘的时候,底部的按钮一同被顶上来,比如说像下图这样(中间的输入框是用ScrollView包裹起来的)
image.png
而实际情况是,当我们按照上面设置的步骤只要在styles文件中设置了
<item name="android:windowTranslucentStatus">true</item>
这个属性就会发现当输入组件获取焦点软件盘弹出时,底部的按钮是不会被顶上来的,这个就有点坑爹了。虽然没什么大的影响,但作为有强迫症的我是不允许出现这种情况的。
- 好了基本上我在使用沉浸式状态栏过程中碰到的一些问题都列在上面了,可能还有别的问题没有碰到,这个以后再说。
问题原因及解决方法
这里参考了一篇博客谈谈“adjustResize”在沉浸式状态栏下的失效问题,该博客完美的解决了上面所说的问题,我就直接将其中的一些内容照抄过来了。
问题原因
- 其实大部分问题都是由
android:fitsSystemWindows="true"
和<item name="android:windowTranslucentStatus">true</item>
这两个属性引起的,因此对应的解决方案主要是针对他们的。
- 上面的第一个问题也就是输入框获取焦点软键盘弹出时ToolBar布局出现延伸的问题,这个问题是由于对ToolBar布局设置了
android:fitsSystemWindows="true"
。 - 上面的第二个和第三个问题是因为我们在styles文件中设置了
<item name="android:windowTranslucentStatus">true</item>
这个属性引起的。我在使用的时候发现只要设置了这个属性,界面布局简短的时候还好,当界面布局比较长以以至于有一部分被弹出的软件盘遮住的时候,不管我使用什么办法,都无法将被遮住的布局滑上来(貌似使用列表组件可以,但是我没试过,而且列表组件限制比较大,比如说界面输入是用Fragment实现的)。
解决方法
- 最佳的解决方法就是不用沉浸式,毕竟现在手机都是5.0以上的,可以直接设置状态栏的蓝色,我们可以直接在项目中设置最低版本为api21版本。
- 针对ToolBar布局延伸,参照上面引用博客,我们自定义布局,将ToolBar包裹进去,并将
android:fitsSystemWindows="true"
属性设置在自定义的布局里面,同时ToolBar的背景颜色也到移动到自定的布局里面,否则状态栏不会显示我们设置好的颜色,代码如下:
<com.mobile.library.view.ImmerseGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?actionBarSize"
app:popupTheme="@style/PopupOverlay"
app:theme="@style/AppBarOverlay">
</android.support.v7.widget.Toolbar>
</com.mobile.library.view.ImmerseGroup>
该自定义布局代码如下:
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.FrameLayout;
/**
* 沉浸式状态栏Toolbar辅助类
* 避免Toolbar设置fitSystemWindows="true"当软键盘弹出时Toolbar被拉伸
*/
public class ImmerseGroup extends FrameLayout {
public ImmerseGroup(Context context) {
super(context);
}
public ImmerseGroup(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public ImmerseGroup(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), 0);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
- 这样便解决了ToolBar布局延伸的问题,但是另外一个问题还没有解决,就是底部的组件如何才能被软键盘顶上来,这里采用了一个工具类来监听android.R.id.content的布局,在软键盘弹出时,动态改变其高度,为软键盘腾出空间。该工具类代码如下:
import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
public class AndroidBug5497Workaround {
public static void assistActivity(View content) {
new AndroidBug5497Workaround(content);
}
private View mChildOfContent;
private int usableHeightPrevious;
private ViewGroup.LayoutParams frameLayoutParams;
private AndroidBug5497Workaround(View content) {
if (content != null) {
mChildOfContent = content;
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = mChildOfContent.getLayoutParams();
}
}
private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
frameLayoutParams.height = usableHeightNow;
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom);
}
}
然后在需要设置的Activity中添加下面一行代码就可以了:
AndroidBug5497Workaround.assistActivity(findViewById(android.R.id.content));
题外话
- 有时候我们嫌沉浸式布局比较麻烦,不想使用,但又想在5.0以下的手机上设置状态栏的颜色,这里安利一个GitHub上面的开源库msdx/status-bar-compat,其实现的状态栏效果和沉浸式状态栏几乎一样(布局并没有延伸到状态栏上面),我已经用在了自己的项目上面,在此感谢该开源库作者的辛勤维护。