zoukankan      html  css  js  c++  java
  • android 6.0导航栏 NavigationBar影响视图解决办法

    在开发app的时候会遇到有些测试手机没有物理按钮,比如最近在做的一个app在小米手机上运行显示效果很好,但是在华为P7手机上显示就乱了,底部的NavigationBar直接覆盖在主视图上,导致按钮无法触发。


    正常效果.jpg

    异常效果.jpg

    解决的方法就是先判断手机是否有物理按钮,然后计算底部的NavigationBar高度,最后设置试图边距。

    public int getNavigationBarHeight() {
    
            boolean hasMenuKey = ViewConfiguration.get(this).hasPermanentMenuKey();
            boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
               if (!hasMenuKey && !hasBackKey) {
                    Resources resources = getResources();
                    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
                    //获取NavigationBar的高度  
                    int height = resources.getDimensionPixelSize(resourceId);
                    return height;
               }
               else{
                    return 0;
              }
    }
    
     getWindow().getDecorView().findViewById(android.R.id.content).setPadding(0, 0, 0, getNavigationBarHeight());
    

    如果设置android.R.id.content的边距,底部是白色背景或者黑色(这个和你用的theme有关,黑色还能接受,白色显得app很不搭)

    <item name="android:windowBackground">@android:color/white</item>
    

    low.jpg

    如果想换其他的颜色,又不想修改windowBackground。那么可以在布局文件中的父试图设置你想要的颜色,然后再显示的时候设置该控件的padding属性。

  • 相关阅读:
    LeetCode456. 132模式
    LeetCode455. 分发饼干
    LeetCode454. 四数相加 II
    LeetCode453. 最小移动次数使数组元素相等
    matchMedia 媒体查询结果
    异常捕获
    常用xpath选择器和css选择器总结
    python-爬虫中的extract()
    Python应用前景广阔,怎么学才能快速上手?
    Python 为什么要有 pass 语句?
  • 原文地址:https://www.cnblogs.com/foxy/p/7929459.html
Copyright © 2011-2022 走看看