Android4.4沉浸状态栏 键盘挤压布局受影响


        最近使用 Android 4.4后特性 沉浸式状态栏,简单说就是透明了状态栏,与ios的状态栏一样;

        设置代码如下(在API 19 及以后)

         

          1.  代码设置,在setContentView()方法之前

                              //透明状态栏                              getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

                              //透明导航栏                  getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);          


            2.  主题style设置

                    true

                  true



       设置完后挺好  颇有成就

               但是   之前的挤压键盘的不挤压布局了  需求是要挤压布局的   

                   后来查资料发现  Android2,1后就存在这个“bug”   全屏的activity 键盘不会挤压布局,

以下可以解决问题    在需要挤压布局的activity setcontentview()方法后 调用方法AndroidBug5497Workaround.assistActivity(this);  问题解决

public class AndroidBug5497Workaround {

    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);
    }

}

 状态栏颜色改变 向下兼容库源码在这里:

https://github.com/jgilfelt/SystemBarTint


关注微信 dream_we90 有更多分享



你可能感兴趣的:(Android4.4沉浸状态栏 键盘挤压布局受影响)