java - 无论我做什么,状态栏通知图标都保持黑色

标签 java android android-notifications android-icons

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

我的问题是我选择了一个图标,将它创建为图像 Assets 并选择白色作为它的颜色,但是无论我在它的 xml 中更改什么值,它看起来都是黑色的(我实际上已经将每个颜色值都更改为白色并且仍然它是黑色的)。
black clock icon black expanded notification icon

我想知道问题的原因是应用程序主题(有一些主题属性我必须覆盖)还是图像 xml 中缺少属性,我知道对于 Lollipop 和最重要的是所有图标都必须是白色的但是 pre 呢? - Lollipop ?如果状态栏颜色为白色,那么如何将图标颜色更改为黑色呢?

我在 api 18 上使用 Theme.MaterialComponents

我的图片 Assets 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>

(夜晚)theme.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>

应用 list

<?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:color/white

您会看到一条消息显示:

Resource references will not work correctly in images generated for this vector icon for API < 21; check generated icon to make sure it looks acceptable

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>

关于java - 无论我做什么,状态栏通知图标都保持黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69120017/

相关文章:

android - 使用 Phonegap 的 Android 实时视频流?

android - 是否可以使用 Glide 来缓存非图像文件,例如视频和 Lottie 动画?

Android WebView 即时崩溃与 shouldInterceptRequest 覆盖

android - 如何让 Android 上的音乐播放器按钮像谷歌播放音乐一样佩戴

java - 私有(private)类数组的长度不可访问

java - 如何使用poi删除excel文件的多行数据

java - Java 中的文本框 keyListener

java - 在字符串中的所有单词 "o", "google",...中找到字母数 "gooogle"的乘积

java - 按下通知按钮时未调用 BroadcastReceiver

android - 如何使用 Scheduler 进行 Http 请求