0%

Android沉浸式布局导致的键盘遮挡问题处理方案

前言

公司项目——沉浸式布局
沉浸式方案:全屏+透明状态栏
问题:键盘遮挡输入框
解决思路:检测键盘是否弹起,根据键盘的弹起与回收,重新绘制界面
解决方案:AndroidBug5497Workaround

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public class AndroidBug5497Workaround {
//该解决方案用于处理 全屏下键盘 问题 :
//当Theme为 @android:style/Theme.Light.NoTitleBar.Fullscreen 时
//android:windowSoftInputMode="adjustResize" 无效
// For more information, see https://code.google.com/p/android/issues/detail?id=5497
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

//紧随setContentView之后调用
public static void assistActivity (Activity activity) {
new AndroidBug5497Workaround(activity);
}

private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;

private AndroidBug5497Workaround(Activity 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();
}

// 获取界面可用高度,如果软键盘弹起后,Activity的xml布局可用高度需要减去键盘高度
private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight();

//判断 当前可用高度和 历史可用高度不同 (即键盘状态发生变化) 时,调整界面高度
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
//将 可用高度的1/4视为键盘高度
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);
//全屏应用 不需要减去 top , r.top 为状态栏高度,r.bottom 为屏幕可用最底部高度
//return r.bottom;
return (r.bottom - r.top);
}
}

参考文档

google-issues-5497(code.google.com)
Android-5497问题解决方案(github.com)
据说能解决5497方案的兼容问题(jianshu.com)

------------本文结束感谢您的阅读------------

Thank you for your accept!