android- 虚拟导航栏挡住底部内容布局

使用2解决

问题:在实现ViewPager+Fragment+侧滑栏的界面时,华为搭载Android5.0以上操作系统的手机出现底部虚拟导航栏挡住布局。如下图所示:

这里写图片描述

问题解决后: 
这里写图片描述

尝试

在实现这个功能的时候,我发现底部虚拟导航栏遮盖布局不同的情况对应不同的解决方法。当没有侧滑功能的时候,主要有一下两种:

1. OnCreate()方法中不能出现下边的代码:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
  • 1

 就是设置导航栏半透明,这会使布局向上向下扩展至整个屏幕,导航栏则覆盖在布局上边,就会导致导航栏挡住布局。有的说法是换成设置状态栏半透明,如下边的代码:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  • 1

 这种做法其实是不好的,属于伤敌一千自伤八百。因为,我们设置这个属性一般是为了实现沉浸式状态栏的,去掉了第一种代码,就不能实现了。比如说我使用了SystemBarTint第三方框架来实现沉浸式状态栏。这时就需要用到方法2了。 
 

2. 在布局的根布局中添加android:fitsSystemWindows=”true”

比如:

 
  1. <LinearLayout

  2. xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:fitsSystemWindows="true

  6. android:orientation="vertical">

  7.  
  8. <View

  9. android:layout_width="match_parent"

  10. android:layout_height="@dimen/theme_divide_height"

  11. android:background="#3D81D6"/>

  12.  
  13. </LinearLayout>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

我们看一下,Android官方API对这个属性的解释:

 
  1. Boolean internal attribute to adjust view layout based on system windows such as the status bar.

  2. If true, adjusts the padding of this view to leave space for the system windows. Will only take effect if this view is in a non-embedded activity.

  3.  
  4. May be a boolean value, such as "true" or "false".

  • 1
  • 2
  • 3
  • 4

翻译: 
 布尔内部属性,基于系统窗口(如状态栏)来调整视图布局。如果为true,则调整此视图的填充,以便为系统窗口留出空间。只有在非嵌入activity中此视图才会生效。 

 这个方法就使系统窗口可以自动调整,可以实现需求。但是如果界面中有侧滑菜单的,并且实现了顶部导航栏透明,和底部导航栏颜色填充的话,就需要下边的方法了。 
 

有效方法

在 style.xml 文件中的项目的主题样式中添加

<item name="android:windowDrawsSystemBarBackgrounds">false</item>
  • 1

我们看一下,Android官方API对这个属性的解释:

 
  1. Flag indicating whether this Window is responsible for drawing the background for the system bars. If true and the window is not floating, the system bars are

  2. drawn with a transparent background and the corresponding areas in this window are filled with the colors specified in statusBarColor and navigationBarColor. Corresponds to FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS.

  3.  
  4. May be a boolean value, such as "true" or "false".

  • 1
  • 2
  • 3
  • 4

翻译: 
 标志是指示此窗口是否负责绘制系统栏的背景。如果真正的窗口不浮,系统栏被画在这个窗口透明背景和相应领域内statusbarcolor和navigationbarcolor指定的颜色。对应于flag_draws_system_bar_backgrounds。 

 可以看出该属性是负责绘制系统栏的背景的,如果真正的窗口被遮盖了,设置true,则会绘制系统栏的背景,使真正的窗口上移,不被遮挡住。 

 如果你的项目兼容的最低版本小于21的话 ,会红线提示错误,虽然可以运行但是代码无效。解决方法是:在提示错误的代码上Alt+Enter,会提示: 

这里写图片描述

 选择第一个,就会自动生成适配Android 21的values文件夹:values-v21,里边有包含该属性的styles.xml文件。之前添加的报错的属性就可以删掉了。当然,你也可以自己新建文件夹,自己实现。如下图: 

这里写图片描述 
 

如果不知道项目的主题样式在哪儿,可以用下边的查找方式:

 打开资源配置文件AndroidManifest.xml,跟进属性 Android:theme=”@style/AppTheme”中的style: 

这里写图片描述 

 tips:android:windowDrawsSystemBarBackgrounds在Android官方API文档版本21以上的可以查到,下边附一个我使用的文档的连接: 
最新版Android官方API文档 

 好了,到此就结束了。希望能帮到大家。

猜你喜欢

转载自blog.csdn.net/jasonhongcn/article/details/84443363
今日推荐