当界面全屏显示的时候 软键盘弹出的时候scrollview失去作用 不能上滑到底部
程序员文章站
2022-04-21 17:50:40
...
Android中有个Bug,在设置getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);为全屏状态下点击输入框显示键盘时,界面并不会重新渲染调整位置,android:windowSoftInputMode="adjustResize"在FullScreen下没有作用。
找了很久才找到解决方法,该方法是在Activity onCreate时通过ViewTreeObserver注册GlobalLayoutListener监听,当全局布局改变时会触发该监听。下面是代码部分:
-
package com.maituo.sdk.util;
-
-
import android.app.Activity;
-
import android.graphics.Rect;
-
import android.view.View;
-
import android.view.ViewGroup.LayoutParams;
-
import android.view.ViewTreeObserver;
-
import android.widget.FrameLayout;
-
-
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 view set.
-
-
// public static void assistActivity (Activity activity) {
-
// new AndroidBug5497Workaround(activity);
-
// }
-
-
private View mContent;
-
private int usableHeightPrevious;
-
private LayoutParams layoutParams;
-
public static void assistView(View v){
-
new AndroidBug5497Workaround(v);
-
}
-
-
public static void assistActivity (Activity activity) {
-
new AndroidBug5497Workaround(activity);
-
}
-
-
private AndroidBug5497Workaround(Activity activity){
-
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
-
mContent = content.getChildAt(0);
-
addGlobalLayoutListener(mContent);
-
-
}
-
//有时通过Activity获取view并不能满足,所以我加了直接传view的一个构造方法满足用到Fragment的情况
-
private AndroidBug5497Workaround(View v) {
-
//FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
-
// mChildOfContent = content.getChildAt(0);
-
addGlobalLayoutListener(v);
-
-
}
-
-
private void addGlobalLayoutListener(View v){
-
mContent = v;
-
mContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
-
public void onGlobalLayout() {
-
possiblyResizeChildOfContent();
-
}
-
});
-
// LayoutParams放在这里有可能得到的为空
-
// LayoutParams = mContent.getLayoutParams();
-
}
-
-
private void possiblyResizeChildOfContent() {
-
int usableHeightNow = computeUsableHeight();
-
if (usableHeightNow != usableHeightPrevious) {
-
layoutParams = mContent.getLayoutParams();
-
int usableHeightSansKeyboard = mContent.getRootView().getHeight();
-
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
-
if (heightDifference > (usableHeightSansKeyboard/4)) {
-
// keyboard probably just became visible
-
layoutParams.height = usableHeightSansKeyboard - heightDifference;
-
} else {
-
// keyboard probably just became hidden
-
layoutParams.height = usableHeightSansKeyboard;
-
}
-
mContent.requestLayout();
-
usableHeightPrevious = usableHeightNow;
-
}
-
}
-
-
private int computeUsableHeight() {
-
Rect r = new Rect();
-
mContent.getWindowVisibleDisplayFrame(r);
-
return (r.bottom - r.top);
-
}
-
-
}
只要调用
AndroidBug5497Workaround.
assistActivity(
activity)或AndroidBug5497Workaround
.
assistView(view)就可以了
上一篇: android 中edittext设置不弹出软键盘
下一篇: EditText 只能输入小数点后两位