美文网首页
键盘弹出不顶a

键盘弹出不顶a

作者: Android苏大强 | 来源:发表于2016-12-27 14:09 被阅读26次

    因为在全屏模式下   是不好使的

    需要在activity    setContentView(R.layout.XXXX)之后调用;

    AndroidBug5497Workaround.assistActivity(this);

    importandroid.app.Activity;

    importandroid.graphics.Rect;

    importandroid.view.View;

    importandroid.view.ViewTreeObserver;

    importandroid.widget.FrameLayout;

    public classAndroidBug5497Workaround {

    // 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 voidassistActivity(Activity activity) {

    newAndroidBug5497Workaround(activity);

    }

    privateViewmChildOfContent;

    private intusableHeightPrevious;

    privateFrameLayout.LayoutParamsframeLayoutParams;

    privateAndroidBug5497Workaround(finalActivity activity) {

    FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);

    mChildOfContent= content.getChildAt(0);

    mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() {

    public voidonGlobalLayout() {

    possiblyResizeChildOfContent(activity);

    }

    });

    frameLayoutParams= (FrameLayout.LayoutParams)mChildOfContent.getLayoutParams();

    }

    private voidpossiblyResizeChildOfContent(Activity activity) {

    intusableHeightNow = computeUsableHeight(activity);

    if(usableHeightNow !=usableHeightPrevious) {

    intusableHeightSansKeyboard =mChildOfContent.getRootView().getHeight();

    intheightDifference = 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 intcomputeUsableHeight(Activity activity) {

    Rect r =newRect();

    mChildOfContent.getWindowVisibleDisplayFrame(r);

    if(r.top==0){

    r.top=getStatusBarHeight(activity);//状态栏目的高度

    }

    return(r.bottom- r.top);

    }

    public intgetStatusBarHeight(Activity activity) {

    intresult =0;

    intresourceId = activity.getResources().getIdentifier("status_bar_height","dimen","android");

    if(resourceId >0) {

    result = activity.getResources().getDimensionPixelSize(resourceId);

    }

    returnresult;

    }

    }

    相关文章

      网友评论

          本文标题: 键盘弹出不顶a

          本文链接:https://www.haomeiwen.com/subject/gxzquttx.html