美文网首页程诺陪你学AndroidAndroid使用场景
android:fitsSystemWindows属性的用法

android:fitsSystemWindows属性的用法

作者: leoryzhu | 来源:发表于2019-05-07 15:20 被阅读334次

    一、属性说明

    fitsSystemWindows属性可以让view根据系统窗口来调整自己的布局,也就是我们在设置布局时是否考虑窗口布局,这里系统窗口包括状态栏、导航栏、输入法等。android:fitsSystemWindows=”true” (触发View的padding属性来给系统窗口留出空间) ,这个属性可以给任何的view设置,设置后view的padding属性就会失效。

    二、应用场景

    不同的android系统版本下,app状态栏的适配

    1、默认效果(未对系统状态栏和导航栏做透明设置时)

    image.png

    2、系统窗口透明效果

    (当设置了透明状态栏(StatusBar)和透明导航栏(NavigationBar)时)

    image.png

    透明状态栏代码设置:

    //布局设置
    <item name="android:windowTranslucentStatus">true</item>
    //或者代码设置
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }
    

    透明导航栏代码设置:
    //布局设置

    <item name="android:windowTranslucentNavigation">true</item>
    //或者代码设置
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }
    

    注意:这里有个关于状态栏和导航栏透明样式的问题,这个是受Android版本决定,4.4以下无透明效果,4.4~5.0是全透明,5.0以上是半透明。我使用的是5.0以上的版本模拟器进行测试的,所以是半透明。

    3、设置fitsSystemWindows属性后效果

    现在就到了我们关键的fitsSystemWindows属性登场了,只要在主题或者根布局中加上android:fitsSystemWindows=”true”效果


    image.png

    三、android:fitsSystemWindows=”true”产生的问题

    我在主题加上android:fitsSystemWindows=”true”后,在弹出PopupWindow时,PopupWindow里面的布局padding会失效,这时在PopupWindow根布局android:fitsSystemWindows=”false”取消根据窗口来调整布局就可以恢复正常
    设置android:fitsSystemWindows=”true”的视图:


    image.png

    添加android:fitsSystemWindows=”false”的视图:


    image.png

    相关文章

      网友评论

        本文标题:android:fitsSystemWindows属性的用法

        本文链接:https://www.haomeiwen.com/subject/wtweoqtx.html