改变状态栏颜色

这样的效果:改变状态栏颜色

方法一.使用SystemBarTintManager :
在需要改变状态栏颜色的Activity中加入代码:

 //只对API19以上版本有效
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        //为状态栏着色
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.color.statusbar);
  @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

方法二:
1.先在values文件夹下的styles.xml 文件中设置如下主题:

   <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <!--在Android 4.4之前的版本上运行,直接跟随系统主题-->
    <style name="TranslucentTheme" parent="AppTheme">
    </style>

2.在values-19中添加styles:

    <style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">false</item>
    </style>

3.在values-21中添加styles:

  <style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">false</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

4.在相应的activity中添加AppTheme:

 android:theme="@style/AppTheme"

5.在xml文件最外层布局中添加:

 android:fitsSystemWindows="true"

这样的效果
隐藏状态栏
在Activity中添加:

      View decorView = getWindow().getDecorView();
        int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(option);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

这样的效果(5.0以上):
沉浸式

 if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
    }

### 更改安卓状态栏颜色的方法 在安卓开发中,更改状态栏颜色可以通过多种方式实现。以下是针对不同场景的具体方法: #### 方法一:通过主题属性设置(适用于 API 21 及以上) 可以在 `styles.xml` 文件中定义主题,并使用 `android:statusBarColor` 属性来指定状态栏颜色[^1]。 ```xml <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- 设置状态栏颜色 --> <item name="android:statusBarColor">@color/status_bar_color</item> </style> ``` 此方法简单易用,适合静态配置的状态栏颜色需求。 --- #### 方法二:动态修改状态栏颜色(Java/Kotlin 实现) 如果需要在运行时动态调整状态栏颜色,则可以按照以下代码操作[^3]。 ##### Java 示例: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = getWindow(); // 清除透明状态栏标志 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // 添加绘制系统栏背景的标志 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); // 设置状态栏颜色 window.setStatusBarColor(ContextCompat.getColor(this, R.color.new_status_color)); } ``` ##### Kotlin 示例: ```kotlin if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { val window = window window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) window.statusBarColor = ContextCompat.getColor(this, R.color.new_status_color) } ``` 这种方法允许开发者根据应用程序的不同页面或逻辑动态改变状态栏颜色[^4]。 --- #### 方法三:使用第三方库实现低版本兼容性 为了支持低于 API 21 的设备,可以借助一些成熟的开源库,比如 [ImmersionBar](https://github.com/gyf-dev/ImmersionBar)[^2]。这些工具提供了更简单的接口用于管理状态栏样式和颜色。 示例代码如下: ```java // 初始化 ImmersionBar 并设置状态栏颜色 ImmersionBar.with(this).statusBarColor(R.color.status_color).init(); ``` 这种方式不仅简化了跨版本适配的工作量,还能提供额外的功能扩展。 --- #### 注意事项 - **深色模式适配**:当启用深色模式时,需注意状态栏图标的对比度问题。可通过 `View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR` 或者类似的机制切换图标颜色风格[^1]。 - **透明状态栏处理**:某些情况下可能希望让内容延伸到状态栏区域,在这种情形下应保留 `FLAG_TRANSLUCENT_STATUS` 标志或者利用 FitSystemWindows 进行布局调整[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值