Android键盘调整大小 [英] Android Keyboard Adjust Resize
问题描述
我正在开发一个包含Activity和Fragment
的应用程序.在片段布局"中,我使用相对布局"作为父布局",在底部和滚动视图之间使用一个按钮. Scrollview
包含editText框.如果我在滚动视图中单击最后一个editTextBox
,我的键盘将隐藏fragment
.我在清单和fragment
中都尝试了adjustpan|adjustresize
,但尚未解决问题.
I am Developing one Application which contains Activity and Fragment
. In Fragment Layout I used Relative Layout as Parent Layout,one button in bottom and in between Scrollview. Scrollview
contains editText Boxes. If i click Last editTextBox
in scrollview My keyboard hides the fragment
. I tried adjustpan|adjustresize
in manifest and also in my fragment
but not yet problem resolved.
推荐答案
其中存在Android的错误.经过很多努力后,我能够提出一个解决此问题的平稳方法.这是一个单行解决方案,但有一些先决条件.一行是:
There are Android's bugs in this. After struggling a lot, I am able to come-up with a smooth workaround to this problem. it is a one line solution, but it has some pre-reqs. The one line is:
AndroidBug5497Workaround.assistActivity(this, R.id.LayoutInScrollView);
您的xml布局必须类似于:
Your xml Layout must be like:
RelativeLayout{
HeaderView{}
ScrollView{
LinearLayout{
@+id/LayoutInScrollView
}
}
FooterView{} // the buttons u want to appear above keyboard
}
如果您不使用全屏显示,则下面的类应该足够了:
If you are not using Full Screen, the following class should be enough:
class AndroidBug5497Workaround{
View svChildLayout;
int originalGravity;
Activity activity;
/**
* @param activity
* @param svChildLayoutId id of the layout that is the first child of the center ScrollView
*/
public static void assistActivity (Activity activity, int svChildLayoutId) {
new AndroidBug5497Workaround(activity, svChildLayoutId);
}
private AndroidBug5497Workaround(Activity activity, int svChildLayoutId) {
this.activity = activity;
svChildLayout = activity.findViewById(svChildLayoutId);
originalGravity = ((ScrollView.LayoutParams)svChildLayout.getLayoutParams()).gravity;
//Add listener
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent2();
}
});
}
private void possiblyResizeChildOfContent2() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard/4)) {
// keyboard probably just became visible
onKeyboardVisible();
} else {
// keyboard probably just became hidden
onKeyboardHidden();
}
usableHeightPrevious = usableHeightNow;
}
}
private void onKeyboardVisible() {
ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams();
params.gravity = Gravity.TOP;
svChildLayout.requestLayout();
final ScrollView parentSv = (ScrollView) svChildLayout.getParent();
parentSv.post(new Runnable() {
@Override
public void run() {
View focusedEditText = activity.getWindow().getCurrentFocus();
parentSv.smoothScrollTo(0, focusedEditText.getTop() );
}
});
}
private void onKeyboardHidden() {
ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams();
params.gravity = originalGravity;
svChildLayout.requestLayout();
}
}
如果使用全屏显示,则需要以下类(从):
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, int svChildLayoutId) {
new AndroidBug5497Workaround(activity, svChildLayoutId);
}
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
View svChildLayout;
int originalGravity;
Activity activity;
private AndroidBug5497Workaround(Activity activity, int svChildLayoutId) {
this.activity = activity;
svChildLayout = activity.findViewById(svChildLayoutId);
originalGravity = ((ScrollView.LayoutParams)svChildLayout.getLayoutParams()).gravity;
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
onKeyboardVisible();
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
} else {
// keyboard probably just became hidden
onKeyboardHidden();
frameLayoutParams.height = usableHeightSansKeyboard;
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
private void onKeyboardVisible() {
ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams();
params.gravity = Gravity.TOP;
svChildLayout.requestLayout();
final ScrollView parentSv = (ScrollView) svChildLayout.getParent();
parentSv.post(new Runnable() {
@Override
public void run() {
View focusedEditText = activity.getWindow().getCurrentFocus();
parentSv.smoothScrollTo(0, focusedEditText.getTop() );
}
});
}
private void onKeyboardHidden() {
ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams();
params.gravity = originalGravity;
svChildLayout.requestLayout();
}
}
这篇关于Android键盘调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!