問題描述
在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é)語(yǔ)
這個(gè)問題大部分國(guó)產(chǎn)ROM都不會(huì)出現(xiàn)弃秆,pixel或者說原生系統(tǒng)做了特殊處理届惋,在深色模式下,如果文字本來是黑色的菠赚,就會(huì)對(duì)所有的view反色處理脑豹,如果是白色(或紅色等)就不會(huì)做反色處理。
修改文字顏色的思路來自:Notification with inverted color with dark mode