Android 解决底部弹出PopWindow时如果有虚拟按键遮挡问题

来源:互联网 发布:淘宝1元秒杀是真的吗 编辑:程序博客网 时间:2024/05/13 09:21

弹出PopWindow时如果手机有虚拟按键可能会出现虚拟按键遮挡住popwindow的布局,导致pop显示不全:
错误显示

解决办法:设置PopWindow显示的Y轴的位置为虚拟按键高度的位置。下面是获取虚拟按键高度的方法:

    /**     * Desc: 获取虚拟按键高度 放到工具类里面直接调用即可     */    public static int getNavigationBarHeight(Context context) {        int result = 0;        if (hasNavBar(context)) {            Resources res = context.getResources();            int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");            if (resourceId > 0) {                result = res.getDimensionPixelSize(resourceId);            }        }        LogUtils.e("虚拟键盘高度"+result);        return result;    }    /**     * 检查是否存在虚拟按键栏     *     * @param context     * @return     */    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)    public static boolean hasNavBar(Context context) {        Resources res = context.getResources();        int resourceId = res.getIdentifier("config_showNavigationBar", "bool", "android");        if (resourceId != 0) {            boolean hasNav = res.getBoolean(resourceId);            // check override flag            String sNavBarOverride = getNavBarOverride();            if ("1".equals(sNavBarOverride)) {                hasNav = false;            } else if ("0".equals(sNavBarOverride)) {                hasNav = true;            }            return hasNav;        } else { // fallback            return !ViewConfiguration.get(context).hasPermanentMenuKey();        }    }    /**     * 判断虚拟按键栏是否重写     *     * @return     */    private static String getNavBarOverride() {        String sNavBarOverride = null;        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            try {                Class c = Class.forName("android.os.SystemProperties");                Method m = c.getDeclaredMethod("get", String.class);                m.setAccessible(true);                sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");            } catch (Throwable e) {            }        }        return sNavBarOverride;    }

然后在显示PonWindow的地方调用:

    popwindow.showAtLocation(findViewById(R.id.layout),                             Gravity.BOTTOM,                             0,                             getNavigationBarHeight(Context context));

修改后的效果:
正确显示

方法参考:http://www.cnblogs.com/ldq2016/p/6905429.html

阅读全文
0 0
原创粉丝点击