这也是个工作中碰到的需求,要求从 Activity B 侧滑退出到 Activity A,就需要将 B 的顶部顶到状态栏,不然侧滑的时候,状态栏会和 B 的页面割裂开来,想当难看。而 B 本身是一个详情页面,底部有一个EditText
控件。
在原本的情况下,EditText
获取焦点后,弹出键盘,会自动把EditText
顶上去,整个页面重新布局。但是当我通过在主题里设置<item name="android:windowTranslucentStatus">true</item>
属性,或者是通过代码设置 B 页面的DecorView
的SystemUiVisibility
的 flag 为SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | SYSTEM_UI_FLAG_LAYOUT_STABLE
来实现页面顶上状态栏效果时,弹出软键盘时页面重新布局的效果就没有了,也就是软键盘会遮挡住EditText
,这是肯定不行的,多方寻找后,找到了一解决方案。
即通过监控android.R.id.content
页面的高度变化,来监听键盘的弹出和收起事件,进而主动重新布局整个DecorView
。
以下是该工具类的的代码本体,取自谈谈“adjustResize”在沉浸式状态栏下的失效问题:
public class AndroidBug5497Workaround {
// For more information, see https://issuetracker.google.com/issues/36911528
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
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() {
@Override
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);
}
}
在 StackOverFlow 上还有一个版本,穿的参数是当前 Activity,但那个方案在有虚拟导航栏的手机上面底部会被虚拟导航栏遮挡住,代码如下,仅供参考,取自Android How to adjust layout in Full Screen Mode when softkeyboard is visible:
public class AndroidBug5497Workaround {
// For more information, see https://issuetracker.google.com/issues/36911528
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
public static void assistActivity (Activity activity) {
new AndroidBug5497Workaround(activity);
}
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private AndroidBug5497Workaround(Activity activity) {
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard/4)) {
// keyboard probably just became visible
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
} else {
// keyboard probably just became hidden
frameLayoutParams.height = usableHeightSansKeyboard;
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
}