android 开发程序员

Android虚拟键(NavigationBar)适配

2016-11-17  本文已影响4431人  Keike
Android虚拟键适配,之前我一直没有在乎过,因为一般我都是自动适应布局,但是最近用到一个界面效果,自己写了一个PopupWindow,造成了类似的效果
未适配图

采用隐藏虚拟键位的方法。
百度一下(原谅我红杏被封了,我就沦落到百度了,鄙视我吧),以下是结果:

``` 

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_HIDDEN);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_SHOW_FULLSCREEN);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

what the fuck   ,上面是什么鬼,试了一下,开始是隐藏的,但是点击布局,又显示出来了,还有建议屏蔽View点击事件,抛弃


+  获取虚拟键高度,然后定位显示布局的位置

      ``` 
public staticPointgetNavigationBarSize(Context context) {
Point appUsableSize =getAppUsableScreenSize(context);
Point realScreenSize =getRealScreenSize(context);
// navigation bar on the right
if(appUsableSize.x< realScreenSize.x) {
return newPoint(realScreenSize.x- appUsableSize.x,appUsableSize.y);
}
// navigation bar at the bottom
if(appUsableSize.y< realScreenSize.y) {
return newPoint(appUsableSize.x,realScreenSize.y- appUsableSize.y);
}
// navigation bar is not present
return newPoint();
}
public staticPointgetAppUsableScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size =newPoint();
display.getSize(size);
returnsize;
}
public staticPointgetRealScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size =newPoint();
if(Build.VERSION.SDK_INT>=17) {
display.getRealSize(size);
}else if(Build.VERSION.SDK_INT>=14) {
try{
size.x= (Integer) Display.class.getMethod("getRawWidth").invoke(display);
size.y= (Integer) Display.class.getMethod("getRawHeight").invoke(display);
}catch(IllegalAccessException e) {}catch(InvocationTargetException e) {}catch(NoSuchMethodException e) {}
}
returnsize;
}
  ``` 



* 测试:虚拟键位正常显示在布局下面,还有LG的手机真的很丑。。。(当然,APP丑这是肯定的)

![适配后图](https://img.haomeiwen.com/i555358/ecb28a26256bee7e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

链接地址:[stackOverFlow](http://stackoverflow.com/questions/20264268/how-to-get-height-and-width-of-navigation-bar-programmatically/29609679#29609679)
顺便说一下:国内技术都是一篇文章处处转,标个原地址能死啊!(鄙视。。。。。)
上一篇下一篇

猜你喜欢

热点阅读