Android键盘调整调整大小

我正在开发一个包含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();
    }
    }

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...