Android 自定义系统通知栏的颜色

本文介绍如何在Android 4.4及以上版本中自定义通知栏背景色。通过修改样式文件并设置透明状态栏颜色,可以轻松实现个性化背景。适用于不同Android版本的适配方案也一并提供。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android 从4.4开始就已经支持自定义通知栏的背景色了!
先来一个效果图
这里写图片描述
这里写图片描述

要实现这样的效果只需要简单的3步

1.在values/style.xml(关于系统适配后面提到)

<style name="customeTheme" parent="AppTheme">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

values-v19/style.xml

<style name="customeTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>

        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>

values-v21/style.xml

<style name="customeTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!--去除ActionBar-->
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <!--设置标题栏-->
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <!--针对5.x以后的系统,要把颜色设置透明,否则会呈现出系统默认的浅灰色-->
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

2.在AndroidManifest.xml中对指定的Activity的theme进行设置例如:

<activity
            android:name=".Main2Activity"
            android:label="@string/title_activity_main2"
            android:theme="@style/customeTheme">
        </activity>

3.在Activity的布局文件中设置android:fitsSystemWindows为true,然后设置你想要的background就可以了例如:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    android:fitsSystemWindows="true"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.cml.ceshilail.Activity1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

详细参考
源码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值