Skip to content

Instantly share code, notes, and snippets.

@XuQK
Created May 2, 2018 02:04
Show Gist options
  • Save XuQK/aaef6a4ab394dc9b2c84d5425fe33b61 to your computer and use it in GitHub Desktop.
Save XuQK/aaef6a4ab394dc9b2c84d5425fe33b61 to your computer and use it in GitHub Desktop.
解决全屏模式下"adjustResize"失效,软键盘会遮挡住"EditText"的问题
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment