隐藏底部虚拟键NavigationBar实现全屏

来源:互联网 发布:单片机LED接线 编辑:程序博客网 时间:2024/04/27 08:55
import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;public class MainActivity extends Activity implements OnClickListener {View main;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);main = getLayoutInflater().inflate(R.layout.activity_main, null);main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);main.setOnClickListener(this);setContentView(main);}@Overridepublic void onClick(View v) {int i = main.getSystemUiVisibility();if (i == View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) {//2main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);} else if (i == View.SYSTEM_UI_FLAG_VISIBLE) {//0main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);} else if (i == View.SYSTEM_UI_FLAG_LOW_PROFILE) {//1main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);}}}


Android 检查设备是否存在 导航栏 NavigationBar
目前也没有可靠的方法来检查设备上是否有导航栏。可以使用KeyCharacterMap.deviceHasKey来检查设备上是否有某些物理键,比如说菜单键、返回键、Home键。然后我们可以通过存在物理键与否来判断是否有NavigationBar(一般来说手机上物理键、NavigationBar共存).
public static int getNavigationBarHeight(Activity activity) {Resources resources = activity.getResources();int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android");//获取NavigationBar的高度int height = resources.getDimensionPixelSize(resourceId);return height;}

上面这段代码,在绝大多数情况下都能获取到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(查询框架是否存在任何物理键盘的任何键盘连接到设备生产给出关键代码的能力。).

那么解决的办法就是:
@SuppressLint("NewApi") public static boolean checkDeviceHasNavigationBar(Context activity) {//通过判断设备是否有返回键、菜单键(不是虚拟键,是手机屏幕外的按键)来确定是否有navigation barboolean hasMenuKey = ViewConfiguration.get(activity).hasPermanentMenuKey();boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);if (!hasMenuKey && !hasBackKey) {// 做任何你需要做的,这个设备有一个导航栏return true;}return false;}


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

在 KitKat以上版本中使用Translucent将Navigation Bar透明化
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0616/3052.html

为Android4.4以上系统的状态栏和导航栏填充颜色
https://github.com/jgilfelt/SystemBarTint

Android状态栏合集-管你透不透明
http://www.open-open.com/lib/view/open1468204363687.html

Android状态栏微技巧,带你真正理解沉浸式模式
http://www.open-open.com/lib/view/open1472112617427.html

Android5.0之Toolbar详解
http://www.open-open.com/lib/view/open1484918068393.html
0 0