Last active
November 6, 2022 07:49
-
-
Save wuyexiong/18cb892646cdb1a82064 to your computer and use it in GitHub Desktop.
Android How to adjust layout in Full Screen Mode when softkeyboard is visible全屏状态下,输入法不能将输入框自动适配屏幕的解决方案
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.graphics.Rect; | |
import android.view.View; | |
import android.view.ViewTreeObserver; | |
import android.widget.FrameLayout; | |
/** | |
* 讨论组:https://code.google.com/p/android/issues/detail?id=5497 | |
* 解决方案:http://stackoverflow.com/questions/7417123 | |
*/ | |
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 | |
// thatalready 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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment