Android 检查设备是否存在 导航栏 NavigationBar

尊重原创、尊重作者,转载请标明出处:

http://blog.csdn.net/lnb333666/article/details/41821149


目前也没有可靠的方法来检查设备上是否有导航栏。可以使用KeyCharacterMap.deviceHasKey来检查设备上是否有某些物理键,比如说菜单键、返回键、Home键。然后我们可以通过存在物理键与否来判断是否有NavigationBar(一般来说手机上物理键、NavigationBar共存).

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public static int getNavigationBarHeight(Activity activity) {  
  2.         Resources resources = activity.getResources();  
  3.         int resourceId = resources.getIdentifier("navigation_bar_height",  
  4.                 "dimen""android");  
  5.         //获取NavigationBar的高度  
  6.         int height = resources.getDimensionPixelSize(resourceId);  
  7.         return height;  
  8.     }  

上面这段代码,在绝大多数情况下都能获取到NavigationBar的高度。所以有人想通过这个高度来判断是否有NavigationBar 是不行的。当然4.0版本以下就不用说了。确认个问题,NavigationBar是4.0以上才有么?

因为设备有物理键仍然可以有一个导航栏。任何设备运行自定义rom时都会设置一个选项,是否禁用的物理键,并添加一个导航栏。看看API:

ViewConfiguration.get(Context context).hasPermanentMenuKey()  有这么一句描述 :Report if the device has a permanent menu key available to the user(报告如果设备有一个永久的菜单主要提供给用户).


android.view.KeyCharacterMap.deviceHasKey(int keyCode) 的描述:Queries the framework about whether any physical keys exist on the any keyboard attached to the device that are capable of producing the given key code(查询框架是否存在任何物理键盘的任何键盘连接到设备生产给出关键代码的能力。).


那么解决的办法就是:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. @SuppressLint("NewApi")   
  2.     public static boolean checkDeviceHasNavigationBar(Context activity) {  
  3.   
  4.         //通过判断设备是否有返回键、菜单键(不是虚拟键,是手机屏幕外的按键)来确定是否有navigation bar  
  5.         boolean hasMenuKey = ViewConfiguration.get(activity)  
  6.                 .hasPermanentMenuKey();  
  7.         boolean hasBackKey = KeyCharacterMap  
  8.                 .deviceHasKey(KeyEvent.KEYCODE_BACK);  
  9.   
  10.         if (!hasMenuKey && !hasBackKey) {  
  11.             // 做任何你需要做的,这个设备有一个导航栏  
  12.             return true;  
  13.         }  
  14.         return false;  
  15.     }  

尊重原创、尊重作者,转载请标明出处:

http://blog.csdn.net/lnb333666/article/details/41821149

要取消 Android 底部导航栏的半透明/毛玻璃效果,可以在 styles.xml 文件中的 AppTheme 样式中添加以下属性: ```xml <item name="android:navigationBarColor">@android:color/transparent</item> <item name="android:windowTranslucentNavigation">false</item> ``` 其中,`android:navigationBarColor` 属性设置为 `@android:color/transparent` 表示将导航栏的颜色设置为透明,`android:windowTranslucentNavigation` 属性设置为 `false` 表示取消导航栏的半透明效果。 完整的 AppTheme 样式如下: ```xml <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- 取消导航栏的半透明效果 --> <item name="android:navigationBarColor">@android:color/transparent</item> <item name="android:windowTranslucentNavigation">false</item> <!-- 其他属性 --> <!-- ... --> </style> ``` 如果想要实现 Android 底部导航栏毛玻璃效果,可以使用 Android 系统提供的 `BlurDrawable` 类来实现。具体实现步骤可以参考以下代码: ```java // 创建一个 BlurDrawable 对象 BlurDrawable blurDrawable = new BlurDrawable(); // 设置模糊半径和颜色 blurDrawable.setBlurRadius(10); blurDrawable.setColor(Color.parseColor("#66000000")); // 将 BlurDrawable 对象设置为导航栏的背景 getWindow().setNavigationBarColor(Color.TRANSPARENT); getWindow().setNavigationBarDividerColor(Color.TRANSPARENT); getWindow().setNavigationBarColor(blurDrawable.getColor()); getWindow().setNavigationBarDividerColor(blurDrawable.getColor()); ``` 其中,`BlurDrawable` 对象的 `setBlurRadius` 方法用于设置模糊半径,`setColor` 方法用于设置毛玻璃颜色。最后将 `BlurDrawable` 对象设置为导航栏的背景即可。 需要注意的是,`BlurDrawable` 类是 Android 11 引入的新类,如果你的应用最低支持 Android 版本不是 11,需要在 build.gradle 文件中添加以下依赖: ```groovy implementation 'androidx.appcompat:appcompat:1.4.0' implementation 'androidx.core:core-ktx:1.6.0' implementation 'androidx.window:window:1.0.0' ``` 并且在代码中导入以下类: ```java import androidx.appcompat.graphics.drawable.BlurDrawable; import androidx.window.WindowManager; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值