关于华为手机自定义view中NavigationBar显示与隐藏造成高度问题的解决办法

  //获取是否存在NavigationBar
   public static boolean checkDeviceHasNavigationBar(Context context) {

       //通过判断设备是否有返回键、菜单键(不是虚拟键,是手机屏幕外的按键)来确定是否有navigation bar
       boolean hasMenuKey = ViewConfiguration.get(context)
               .hasPermanentMenuKey();
       boolean hasBackKey = KeyCharacterMap
               .deviceHasKey(KeyEvent.KEYCODE_BACK);

       if (!hasMenuKey && !hasBackKey) {
           // 做任何你需要做的,这个设备有一个导航栏
           return true;
       }
       return false;
   }

   //获取是否存在NavigationBar
public static boolean checkHuaWeiDeviceHasNavigationBar(Context context) {
   boolean hasNavigationBar = false;
   try {
      Resources rs = context.getResources();
      int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
      if (id > 0) {
         hasNavigationBar = rs.getBoolean(id);
      }
      Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
      Method m = systemPropertiesClass.getMethod("get", String.class);
      String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
      if ("1".equals(navBarOverride)) {
         hasNavigationBar = false;
      } else if ("0".equals(navBarOverride)) {
         hasNavigationBar = true;
      }
   } catch (Exception e) {

   }
   return hasNavigationBar;
}

   //NavigationBar状态是否是显示
   public boolean isNavigationBarShow() {
       Activity mContext = (Activity) context;
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
           Display display = mContext.getWindowManager().getDefaultDisplay();
           Point size = new Point();
           Point realSize = new Point();
           display.getSize(size);
           display.getRealSize(realSize);
           return realSize.y != size.y;
       } else {
           boolean menu = ViewConfiguration.get(context).hasPermanentMenuKey();
           boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
           if (menu || back) {
               return false;
           } else {
               return true;
           }
       }

}

//获取NavigationBar高度

	private int getNavigationBarHeight() {
   	 Resources resources = context.getResources();
   	 int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
   	 int height = resources.getDimensionPixelSize(resourceId);
   	 Log.v("dbw", "Navi height:" + height);
  	  return height;
	}
之后重写onMeasure方法在该方法中进行判断是否存在NavigationBar,若存在判断NavigationBar的显示与隐藏,之后重新计算高度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值