Translucent System Bar 半透明状态栏的使用

                              Translucent System Bar 半透明状态栏的使用

一起源
     在Android 4.4之后便引入了半透明状态栏的概念,此时状态栏最顶层有一层阴影,也就是平时我们所说的渐变效果。而在5.0以上谷歌又做了调整,不仅支持设置渐变效果,还是支持设置完全纯色的效果。
二分类分为两种
      1适合没有导航栏  toolBar   actionBar 的背景显示一张图片   包括状态栏效果如下
      
       2是把状态栏 和toolbar设置成一样的颜色   如QQ音乐
三实现          主要设置values里的style       然后设置给activity的theme
   1 图片背景的
        为了适配多种安卓版本     分别创建三种资源
        在valuesvalues-v19values-v21的style.xml都设置一个 Translucent System Bar 风格的Theme
    values/style.xml
   
<style name="ImageTranslucentTheme" parent="AppTheme">
    <!--在Android 4.4之前的版本上运行,直接跟随系统主题-->
</style>
    values-v19/style.xml
<style name="ImageTranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>
     values-v21/style.xml
<style name="ImageTranslucentTheme" 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>
    然后在AndroidManifest.xml中对指定Activity的theme进行设置
  
<activity
    android:name=".ui.ImageTranslucentBarActivity"
    android:label="@string/image_translucent_bar"
    android:theme="@style/ImageTranslucentTheme" />
     在该Activity的布局文件中设置背景图片,同时,需要把android:fitsSystemWindows设置为true
     android:fitsSystemWindows="true"必须设置
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/env_bg"
    android:fitsSystemWindows="true">
 
</RelativeLayout>
   效果:



  
   使用带有ToolBar的设置和状态栏一样的颜色   类似QQ音乐  只需要修改values_v21/style.xml其他资源包下的还是一样的
<style name="ColorTranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:statusBarColor">@color/color_green</item>
</style>
   然后去toolBar布局设置toolBar的颜色  和上边状态栏颜色一致
    android:fitsSystemWindows="true"放最外层     toolBar的颜色也为color_green
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.wbxu.mymaterialdemo.MainActivity"
    android:fitsSystemWindows="true"
>
    <!--android:background="@drawable/a"-->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolBar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/color_green"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ToolbarPopupTheme"
        >
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </android.support.v7.widget.Toolbar>
</FrameLayout>
     效果:
   

四总结:
     方式一适用于app中没有导航栏,且整体的背景是一张图片的界面;
     方式二适用于app中导航栏颜色为纯色的界面;
     android:fitsSystemWindows设置要在布局文件中,不要到theme中设置;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想要在系统UI中将状态栏设置为透明,可以使用以下方法: 1. 在 `res/values/styles.xml` 中添加以下代码: ``` <style name="TransparentSystemBars" parent="@android:style/Theme.DeviceDefault.NoActionBar"> <item name="android:windowTranslucentStatus">true</item> <item name="android:statusBarColor">@android:color/transparent</item> </style> ``` 这些代码将定义一个新的主题,其中包含窗口的状态栏透明和状态栏颜色透明。 2. 在 `AndroidManifest.xml` 文件中将应用程序的主题设置为新定义的主题: ``` <application ... android:theme="@style/TransparentSystemBars"> ... </application> ``` 这些代码将应用程序的主题设置为新定义的主题,其中包含透明状态栏状态栏颜色。 3. 在 `onCreate` 方法中设置状态栏透明: ``` if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.setStatusBarColor(Color.TRANSPARENT); } ``` 这些代码将启用透明状态栏,并将状态栏的背景颜色设置为透明。 请注意,这些代码只在 Android 5.0(API级别21)及以上版本上起作用。如果你的应用程序运行在旧的 Android 版本上,则无法使用这些代码。 希望这些代码能够帮助你在系统UI中实现透明状态栏。如果问题依然存在,请提供更多的细节和错误信息,以便我更好地帮助你。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值