大飞

大飞 关注TA

挑战一切!

大飞

大飞

关注TA

挑战一切!

  •  世界的顶端
  • 混口饭吃
  • 写了333,609字

该文章投稿至Nemo社区   Android  板块 复制链接


Android 虚拟按键适配工具

发布于 2018/02/06 16:48 2,183浏览 0回复 3,213

4EAF4C24AF8EB8739A9AAC91D6D1DAB9


适用于沉浸状态栏,该代码需要放置于设置沉浸状态栏后面,代码详见:

https://github.com/tzz2015/commonLib


package com.zyf.fwms.commonlibrary.utils;

import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

/**
 * 创建 by lyf on 2018/2/5.
 * 描述:
 */

public class VirtualKeyUtils {

    private static VirtualKeyUtils instance;
    public static VirtualKeyUtils getInstance(){
        if(instance==null){
            synchronized (VirtualKeyUtils.class) {
                if (instance == null) {
                    instance = new VirtualKeyUtils();
                }
            }
        }
        return instance;
    }


    private View mChildOfContent;
    private int usableHeightPrevious;
    private ViewGroup.LayoutParams frameLayoutParams;

    public void init(View content) {
        mChildOfContent = content;
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
        frameLayoutParams = mChildOfContent.getLayoutParams();
    }

    public void possiblyResizeChildOfContent() {
        if(mChildOfContent==null) return;
        int usableHeightNow = computeUsableHeight();
        //int usableHeightNow = AutoUtils.displayHeight;

        if (usableHeightNow != usableHeightPrevious) {
            //如果两次高度不一致


//            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
//            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
//            if (heightDifference > (usableHeightSansKeyboard / 4)) {
//                // keyboard probably just became visible
//                isKeyBordVisiable=true;
//            } else {
//                // keyboard probably just became hidden
//                isKeyBordVisiable=false;
//            }
            //将计算的可视高度设置成视图的高度
            frameLayoutParams.height = usableHeightNow;
            mChildOfContent.requestLayout();//请求重新布局
            usableHeightPrevious = usableHeightNow;
        }
    }

    /**
     * 用于隐藏状态栏的页面
     */
    public void noBarResizeChildOfContent(){
        if(mChildOfContent==null) return;
        mChildOfContent.postDelayed(new Runnable() {
            @Override
            public void run() {
                frameLayoutParams.height = AutoUtils.displayHeight;
                mChildOfContent.requestLayout();//请求重新布局
            }
        },300);

    }

    private int computeUsableHeight() {
        //计算视图可视高度
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);
    }
}

如果隐藏状态栏

/**
* 隐藏状态栏
*
* @param context
*/
public static void hideStatusLan(Activity context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
VirtualKeyUtils.getInstance().noBarResizeChildOfContent();
}

}
本文标签
 {{tag}}
点了个评