问题描述
在pixel的Q系统手机上,设置深色模式后,存在通知中的图片反色的问题。比如以下代码:
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="随时随地 听我想听"
android:textColor="#000"
android:textSize="15sp" />
</RelativeLayout>
gradle文件设置:
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系统非深色模式下显示为:
![](https://img.haomeiwen.com/i4272648/487dc9f24f3b54cb.png)
打开深色模式:
![](https://img.haomeiwen.com/i4272648/092650092281f06b.png)
可以看到,图片明显发生了反色。
下面提供两个解决方案。
方案一 升级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"
}
显示如下:
![](https://img.haomeiwen.com/i4272648/bae1f43c46a230d3.png)
虽然图片正常了,但是文字因为我们设置的是黑色,所以也看不到了,需要单独处理
方案二 修改文字颜色
方案一需要修改targetSdkVersion
,这个影响无疑是很大的。在不修改targetSdkVersion
的前提下,我们可以通过修改文字颜色来避免这个问题。
下面把通知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="随时随地 听我想听"
android:textColor="#f00"
android:textSize="15sp" />
显示正常:
![](https://img.haomeiwen.com/i4272648/a92d51cb0925ae06.png)
结语
这个问题大部分国产ROM都不会出现,pixel或者说原生系统做了特殊处理,在深色模式下,如果文字本来是黑色的,就会对所有的view反色处理,如果是白色(或红色等)就不会做反色处理。
修改文字颜色的思路来自:Notification with inverted color with dark mode
网友评论