关于使用fitsSystemWindows 的大坑

fitsSystemWindows通常我们用来实现各版本来状态栏的适配(API19以上我们才能修改状态栏),但在使用过程中,发现使用不当会给自己挖下很大的坑。先看一下官方描述

Boolean internal attribute to adjust view layout based on system windows such as the status bar. 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.
May be a boolean value, such as "true" or "false".

布尔内部属性,用于根据系统窗口(如状态栏)调整视图布局。如果为true,则调整此视图的填充以为系统窗口留出空间。仅在此视图位于非嵌入活动中时才会生效。


fitsSystemWindows属性的使用,要想生效必须与透明状态栏或透明底部导航栏一起使用方可生效。

重点 fitsSystemWindows = true时,会使我们的布局或View预留出系统状态栏底部导航栏空间,即给View自动添加上padding值,这时候我们给View设置的padding属性则无效

坑一

1.不设置透明状态栏或透明底部导航栏,虽然不生效,但在部分机型上EditText长按粘贴时会出现适配问题,如华为手机上弹出紧包裹粘贴文案的弹窗,模拟器上出现布局不居中弹窗等问题
2.只要设置了透明状态栏或透明底部导航栏,即可生效,但我们设置的padding属性将不再生效

在这里插入图片描述

 <item name="android:fitsSystemWindows">true</item>
<item name="android:windowTranslucentStatus">true</item>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="50dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/holo_blue_bright">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

fitsSystemWindows = false时,我们的布局会占据整个系统窗口,不会给状态栏、底部导航栏预留空间,当然前提是:必须设置透明状态栏或透明底部导航栏,这样方可生效。(通常我们使用其实现沉浸式)

坑二:

如果我们设置了底部透明状态栏时,这时候我们布局占据整个系统窗口,如果我们布局底部存在tab切换按钮时,如果我们的手机是虚拟底部导航栏。导航栏会覆盖在我们的tab按钮上,导致我们的tab按钮无效

不过fitsSystemWindows = false时,不管是否生效,我们给布局设置的padding属性是生效的,另外对于EditText的长按粘贴也正常了。

在这里插入图片描述

 <item name="android:fitsSystemWindows">false</item>
 <item name="android:windowTranslucentStatus">true</item>
 <item name="android:windowTranslucentNavigation">true</item>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="50dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/holo_blue_bright">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

对于fitsSystemWindows可真的小心使用,个人不建议使用fitsSystemWindows=true,至于fitsSystemWindows=false我们可视情况使用,如果想达到不通过设置fitsSystemWindows而实现沉浸式效果,可参考StatusBarUtils沉浸式状态栏适配(第二种实现方式)

以上部分 转载 :https://www.jianshu.com/p/9c6cde59575e

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值