状态栏通知图标无论我做什么都是黑色的。

3

我开始制作前台服务,根据文档,我需要将通知与任何前台服务相关联,以便让用户跟踪我的服务是否正在工作。

我的问题是我选择了一个图标,将其创建为图像资产,并选择白色作为其颜色,但无论我改变它的xml中的哪个值,它都显示为黑色(我已经将每个颜色值都更改为白色,但仍然是黑色)。 black clock iconblack expanded notification icon

我想知道问题的原因是应用程序主题(是否有某些主题属性需要覆盖),还是图像xml中缺少属性。我知道对于Lollipop及以上版本,所有图标必须是白色的,但早期版本呢?如果状态栏颜色是白色,那么如何将图标颜色更改为黑色?

我正在使用api 18上的Theme.MaterialComponents

我的图像资产xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24"
    android:tint="@color/white"
    app:tint="@color/white"
    android:alpha="0.8">
  <group android:scaleX="1.2"
      android:scaleY="1.2"
      android:translateX="-2.4"
      android:translateY="-2.4">
    <path
        android:fillColor="@android:color/white"
        android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8z"/>
    <path
        android:fillColor="@android:color/white"
        android:pathData="M12.5,7H11v6l5.25,3.15 0.75,-1.23 -4.5,-2.67z"/>
  </group>
</vector>

我的服务中的通知代码

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_clock)
            .setContentTitle(getText(R.string.starting_notification_title))
            .setContentText(getText(R.string.starting_notification_desc))
            .setPriority(priority);
Notification notification = builder.build();
startForeground(startId,notification);

更新

(正常) themes.xml

   <resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.FacebookTest" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
    <style name="number_picker_custom_style" parent="@android:style/Theme" />
    <style name="Theme.FacebookTest.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="Theme.FacebookTest.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="Theme.FacebookTest.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
    <style name="ShapeAppearanceOverlay.App.CornerSize50Percent" parent="">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">50%</item>
    </style>
    </resources>

(夜间)主题.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.FacebookTest" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_200</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/black</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_200</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>

    <style name="Theme.FacebookTest.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="Theme.FacebookTest.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="Theme.FacebookTest.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

应用清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pretest">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <!--android:maxSdkVersion="28"
    android:hardwareAccelerated="false"
        android:largeHeap="true"-->
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MaterialComponents">
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
        <meta-data android:name="com.facebook.sdk.ClientToken" android:value="@string/facebook_client_token"/>
        <activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name" />
        <activity
            android:name="com.facebook.CustomTabActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="@string/fb_login_protocol_scheme" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.pretest.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.FacebookTest.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".services.BootBroadcastReceiver"  android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
        <service android:name=".services.AlarmSettingService" android:exported="false"/>
        <receiver
            android:name=".services.AlarmReceiver"
            android:enabled="true"
            android:exported="true" />
    </application>

</manifest>

更新

我的可绘制文件(我正在使用ic_clock,似乎它旁边没有任何版本代码)
drawables


它是特定于一个Android版本还是在所有版本上都会发生? - Mayur Gajra
根据我在虚拟机上测试的情况,大多数早期的Android版本(<21)都是这样的。在实际的手机上,我只有一个API 18的手机可以截屏。至于Lollipop之后的版本,根据我的阅读,它默认为白色(我在API 30的实际手机上尝试过)。无论如何,我对如何在不同状态栏颜色下控制其颜色感兴趣。 - Omar Shawky
2个回答

1
我查看了你的代码并发现问题出在你使用的颜色资源上。如果你将鼠标悬停在@android:color/white上,你会看到出现以下信息:
引用资源在 API < 21 的矢量图标生成中无法正常工作;请检查生成的图标以确保其外观可接受。

vector api 18 issue

所以您需要将这些颜色资源更改为直接的颜色代码。例如:android:fillColor="#FFFFFF"

您的最终可绘制对象将类似于此:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:width="24dp"
    android:height="24dp"
    android:alpha="0.8"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <group
        android:scaleX="1.2"
        android:scaleY="1.2"
        android:translateX="-2.4"
        android:translateY="-2.4">
        <path
            android:fillColor="#FFFFFF"
            android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8z" />
        <path
            android:fillColor="#FFFFFF"
            android:pathData="M12.5,7H11v6l5.25,3.15 0.75,-1.23 -4.5,-2.67z" />
    </group>
</vector>

不好意思,我使用了你的代码,但它仍然是黑色的。你可以尝试在模拟器中运行它,你会发现它仍然很奇怪黑色的 :( - Omar Shawky
@OmarShawky 我已经尝试在API级别为18的模拟器上运行了这个程序,它可以正常工作。你是否使用了我提供的图标?如果是的话,你能否想到任何可能会影响结果的相关代码? - Mayur Gajra
正如我所说,我正在使用Theme.MaterialComponents主题,如果这有助于解决问题,我将分享我的样式和应用程序清单,只需要给我一分钟。 - Omar Shawky
1
我认为@MayurGajra的答案是正确的,也许你只需要先卸载该应用程序。 - lukman nudin
@lukmannudin,老实说,我也这么认为,这让我很好奇是什么原因导致了这种情况,而且我非常感激他仍然在努力帮助:D - Omar Shawky
显示剩余7条评论

0

随着Mayur提出的解决方案,我注意到了一个我从未注意过的警告。

我在检查drawables-*dpi文件夹时发现了问题,它们没有按照xml文件中的指示更新,而且似乎Notification.setSmallIcon使用了其中一个drawables-*dpi,而不是遵循xml文件中的指示。
所以,我简单地删除了时钟资源,并从头开始创建,以获得新更新的*dpi.png图片。

我不确定这是否只是图像资产未更新*dpi.png图像的问题,还是setSmallIcon方法中的api行为,但这解决了我的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接