我正在开发一个包含Activity和Fragment的应用程序.在片段布局中,我使用相对布局作为父布局,在底部和Scrollview之间使用一个按钮. Scrollview包含editText Boxes.如果我在scrollview中单击Last editTextBox我的键盘隐藏了片段.我在manifest和try片段中尝试过adjustpan | adjustresize但尚未解决问题.
解决方法
这有
Android的错误.经过多次努力之后,我能够针对这个问题顺利解决问题.它是一个单行解决方案,但它有一些预先要求.一行是:
AndroidBug5497Workaround.assistActivity(this,R.id.LayoutInScrollView);
您的xml布局必须如下:
RelativeLayout{ HeaderView{} ScrollView{ LinearLayout{ @+id/LayoutInScrollView } } FooterView{} // the buttons u want to appear above keyboard }
如果您不使用全屏,则以下类应该足够:
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(); } }
如果您使用全屏,则需要以下类(从windowSoftInputMode=”adjustResize” not working with translucent action/navbar修改):
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(); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。