Android虚拟按键栏处理方案
2017-09-12 10:05
120 查看
最近在公司的项目中 , 华为用户反馈出了一个问题 , 华为手机底部有虚拟按键栏把应用的底部内容遮挡住了 , 现在已经把这个问题解决了 , 记录一下,给各位遇到相同问题的童鞋做一下参考.
处理虚拟按键栏工具类:
调用工具类方法 , 获取虚拟按键高度:
处理虚拟按键栏工具类:
public class ScreenUtils { //获取虚拟按键的高度 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); } } 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");//获取资源id 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; } }
调用工具类方法 , 获取虚拟按键高度:
//处理虚拟按键 //判断用户手机机型是否有虚拟按键栏 if(ScreenUtils.hasNavBar(getApplicationContext())){ setNavigationBar(); } //处理虚拟按键 private void setNavigationBar() { int barHeight = ScreenUtils.getNavigationBarHeight(getApplicationContext()); LinearLayout.LayoutParams barParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); TextView tv = new TextView(this); tv.setHeight(barHeight); tv.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); tv.setBackgroundColor(Color.BLACK); llNavigationBar.addView(tv,barParams); }
相关文章推荐
- Android 虚拟按键栏的处理方案
- Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案
- Android键盘面板冲突 布局闪动处理方案
- Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案
- 解决Android键盘面板冲突 布局闪动处理方案
- Android中处理Touch Icon的方案
- Android键盘面板冲突 布局闪动处理方案
- Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案
- Android中PopupWindow设置背景的方案处理
- android 6.0 wifi 随机mac地址存入mvram临时处理方案
- Android WebView 常见问题及处理方案
- Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案
- Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案
- Android键盘面板冲突 布局闪动处理方案
- 【Android】Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案
- android应用程序启动时短暂白屏或者黑屏的解决处理方案
- Android网络请求处理异步回调风险方案
- Android H5微信支付 webview设置referer 请求头无效问题解释和详细处理方案
- Android分辨率处理方案
- Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案