美文网首页
Android 沉浸式状态栏+状态栏暗色

Android 沉浸式状态栏+状态栏暗色

作者: 懇_ | 来源:发表于2019-04-17 15:16 被阅读0次

    沉浸式状态栏在网上基本是可以一步实现,但是我在开发中因为使用白色全局背景,所以第二步需要使状态栏使用暗色主题来在白色背景上显示。

    在Style中设置Theme

    为保证所有API版本的使用,需要设置三份style文件,values、values-v19、values-21

    //valuse
    <style name="TranslucentTheme" parent="AppTheme">
    </style>
    
    // values-v19。v19 开始有 android:windowTranslucentStatus 这个属性
    <style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="android:windowTranslucentStatus">true</item>
            <item name="android:windowTranslucentNavigation">true</item>
    </style>
    
    // values-v21。5.0 以上提供了 setStatusBarColor()  方法设置状态栏颜色。
    <style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">false</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
    

    引用Android 沉浸式状态栏的实现

    设置暗色状态栏

    因为状态栏默认是亮色背景,所以在使用白色背景的时候状态栏就和背景融为一体了

    this.getWindow().getDecorView().setSystemUiVisibility
    (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    

    相关文章

      网友评论

          本文标题:Android 沉浸式状态栏+状态栏暗色

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