Android笔记之获得虚拟按键、状态栏高度

来源:互联网 发布:ida软件 编辑:程序博客网 时间:2024/06/05 21:29

开发中经常会需要计算各种布局高度,其中就包括了虚拟按键的高度和状态栏高度。

在使用PopWindow时,如果选在从底部显示,这时如果没有计算虚拟按键高度就会导致在开启了虚拟按键的手机上PopWindow被虚拟按键遮挡。如果选择全屏显示,如果没有计算状态栏高度便会导致在一些Rom中强制显示状态栏,而PopWindow显示不全。

以下是对应的代码。

/**     * 获取 虚拟按键的高度     * @param context     * @return     */    public static  int getBottomStatusHeight(Context context){        int totalHeight = getDpi(context);        int contentHeight = MyApplication.windowHeight;        return totalHeight  - contentHeight;    }
/**     * @return 通过反射R得到状态栏高度     */    public static int getStatusBarHeight() {        Class<?> c = null;        Object obj = null;        Field field = null;        int x = 0, sbar = 0;        try {            c = Class.forName("com.android.internal.R$dimen");            obj = c.newInstance();            field = c.getField("status_bar_height");            x = Integer.parseInt(field.get(obj).toString());            sbar = MyApplication.myApplication.getResources().getDimensionPixelSize(x);        } catch (Exception e1) {            e1.printStackTrace();        }        return sbar;    }

0 0