[英]Notification Icon not display properly with Xamarin Android on Lollipop
我在設置通知圖像以正確顯示時遇到問題。
下面是原始圖像,文件路徑:
myApp\\Resources\\drawable\\Icon.png
尺寸:72x72 像素,32 位深度
問題:
它顯示如下。
下面是代碼。 該代碼似乎可以正常顯示它的先前版本。
public class MyServiceConnection : Java.Lang.Object, IServiceConnection
{
private string _appName;
public MyServiceConnection(string appName)
{
_appName = appName;
}
public void OnServiceConnected(ComponentName name, IBinder service)
{
var intent = new Intent(Application.Context, typeof(ManifestActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(Application.Context, 0, intent, PendingIntentFlags.UpdateCurrent);
var notificationBuilder = new NotificationCompat.Builder(Application.Context)
.SetSmallIcon(Resource.Drawable.Icon)
.SetContentTitle(_appName)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManager.FromContext(Application.Context);
var notification = notificationBuilder.Build();
notificationManager.Notify(1, notification);
var serviceBinder = service as MyService.Binder;
serviceBinder.StartForeground(1, notification);
}
public void OnServiceDisconnected(ComponentName name)
{
}
}
環境:
android:minSdkVersion="15" android:targetSdkVersion="27"
更新
正如我所提到的,使用相同的代碼和相同的 png 文件,現在在單獨的 apk 上工作。 一旦為第二個分支創建了代碼,問題就會出現在第二個分支上。
更新 2
我通過在下面更改來修復它
Project Properties -> Android Manifest -> Target Android Version to "Use Compile using SDK version"
結果如下:
android:minSdkVersion="15"
“使用使用 SDK 版本編譯”是什么意思? 安裝在 Lollipop 上時使用的是什么目標版本?
因為谷歌對android5.0及以上(不包括5.0)做了限制,為了統一系統風格。 之后狀態欄的圖標就不能隨意使用色彩豐富的圖片了。 它只能以兩種顏色出現,白色和透明。
您可以通過兩種方式進行:
1.通過降低targetSdkVersion方法顯示彩色圖標,但不支持降低targetSdkVersion方法。(不推薦)
2.分別設置setSmallIcon(制作另一個透明背景的白色圖標)
if(Android.OS.Build.VERSION.SdkInt> BuildVersionCodes.Lollipop){
// white icon with a transparent background
notificationBuilder.SetSmallIcon(Resource.Drawable.ic_aphla_logo);
} else {
// icon image
notificationBuilder.SetSmallIcon(Resource.Drawable.ic_logo);
}
可以參考這個: Android 5 Lollipop 通知欄圖標變白
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.