android Q深色模式下通知的圖片反色的問題

問題描述

在pixel的Q系統(tǒng)手機(jī)上,設(shè)置深色模式后,存在通知中的圖片反色的問題。比如以下代碼:

public class MainActivity extends AppCompatActivity {
    private static final String CHANNEL_ONE_ID = "sollian";
    private static final String CHANNEL_ONE_NAME = "sollian";

    private NotificationManagerCompat notificationMgr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        notificationMgr = NotificationManagerCompat.from(this);
    }

    public void showNotification(View view) {
        setNotification("title", "desc");
    }

    public void hideNotification(View view) {
        notificationMgr.cancel(1);
    }

    void setNotification(String title, String desc) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
                    CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);
            NotificationManager nm = getSystemService(NotificationManager.class);
            nm.createNotificationChannel(notificationChannel);
        }

        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_layout);
        Notification.Builder builder = new Notification.Builder(this, CHANNEL_ONE_ID)
                .setContentTitle(title)
                .setContentText(desc)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setCustomBigContentView(remoteViews)
                .setSmallIcon(R.drawable.logo);
        notificationMgr.notify(1, builder.build());
    }
}

布局文件:

<?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="110dp">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="110dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/logo" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="8dp"
        android:layout_toRightOf="@+id/icon"
        android:text="隨時(shí)隨地 聽我想聽"
        android:textColor="#000"
        android:textSize="15sp" />
</RelativeLayout>

gradle文件設(shè)置:

compileSdkVersion 26
    buildToolsVersion "26.0.3"
    defaultConfig {
        applicationId "com.sollian.notificationdemo"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

pixel Q系統(tǒng)非深色模式下顯示為:


圖1 pixel Q系統(tǒng)非深色模式下的顯示

打開深色模式:


圖2 深色模式下的顯示

可以看到,圖片明顯發(fā)生了反色。

下面提供兩個(gè)解決方案。

方案一 升級(jí)targetSdkVersion

將targetSdkVersion改為29,如下:

compileSdkVersion 29
    buildToolsVersion "29.0.3"
    defaultConfig {
        applicationId "com.sollian.notificationdemo"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

顯示如下:


圖3 targetSdkVersion=29

雖然圖片正常了,但是文字因?yàn)槲覀冊(cè)O(shè)置的是黑色,所以也看不到了,需要單獨(dú)處理

方案二 修改文字顏色

方案一需要修改targetSdkVersion,這個(gè)影響無疑是很大的。在不修改targetSdkVersion的前提下,我們可以通過修改文字顏色來避免這個(gè)問題。
下面把通知title的顏色改為紅色:

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="8dp"
        android:layout_toRightOf="@+id/icon"
        android:text="隨時(shí)隨地 聽我想聽"
        android:textColor="#f00"
        android:textSize="15sp" />

顯示正常:


圖4 修改標(biāo)題顏色

結(jié)語

這個(gè)問題大部分國產(chǎn)ROM都不會(huì)出現(xiàn),pixel或者說原生系統(tǒng)做了特殊處理,在深色模式下,如果文字本來是黑色的,就會(huì)對(duì)所有的view反色處理,如果是白色(或紅色等)就不會(huì)做反色處理。

修改文字顏色的思路來自:Notification with inverted color with dark mode

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容