起因
我们在这样设置了之后,键盘就会将布局弹起,一切都随着预想的事情开始慢慢发生,但是事与愿违,键盘铁板钉钉的挡住了我的EditText!没事,大佬们说了,需要设置ScrollView。get,立即去修改布局,然而又是徒劳无功。
起初搜了下并没有满意的答案,因为我一开始围绕着如何将键盘顶起开始搜的,但是后来发现不是不会顶起,而是可能没有生效,于是找到了一篇文章,这个文章解答了原因。传送门!
但是这位大佬并没有告诉我们怎么解决,而是用了取代监听键盘弹起的方法,这个我们都有用过,但是目前这种情况,我需要的还是顶起布局的操作,于是乎,顺着这个思路搜索,一下子就有了答案。
解决
传送门!
这个给了解决方法,我也拷贝了一份。以下代码拷贝一下到项目中,在对应的Activity的setContentView之后调用AndroidBug5497Workaround.assistActivity(this)即可。里面也有stackflow里的解释,有兴趣的可以自己去看下。
public class AndroidBug5497Workaround {
// 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
public static void assistActivity(Activity activity) {
new AndroidBug5497Workaround(activity);
}
private Activity activity;
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private AndroidBug5497Workaround(Activity activity) {
this.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();
//这个判断是为了解决19之前的版本不支持沉浸式状态栏导致布局显示不完全的问题
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
usableHeightSansKeyboard -= statusBarHeight;
}
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 frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
//这个判断是为了解决19之后的版本在弹出软键盘时,键盘和推上去的布局(adjustResize)之间有黑色区域的问题
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return (r.bottom - r.top) + statusBarHeight;
}
return (r.bottom - r.top);
}
问题就此解决!最近有点忙,得加大自己的阅读量了呀。。。