1
你好stackoverflow社區。我正在使用NotificationCompat處理通知功能。我希望通知圖標對於Android < v11和適用於Android> = 11的光線較暗。我遵循設計準則並創建了圖標,但我沒有運氣自動顯示正確的圖標。Android通知圖標 - 開關黑暗和白色
我發現的所有教程都使用setIcon(R.drawable.icon_name_here)設置圖標。但對可繪製資源進行硬編碼只允許您設置一個圖標。所以如果它是一個黑暗的圖標,它看起來不錯在Android < v11,但在Android> = v11中看起來很荒謬。
我嘗試添加在res /價值/ styles.xml以下樣式:
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
<item name="icon_notification">@drawable/ic_notification_dark</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
這裏是我的RES /值-V11/styles.xml:
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 11 theme customizations can go here. -->
<item name="icon_notification">@drawable/ic_notification_light</item>
</style>
這裏是我的RES /價值/ attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AppBaseTheme">
<attr name="icon_notification" format="reference" />
</declare-styleable>
</resources>
最後,這裏是該圖標在NotificationCompat類設置:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.attr.icon_notification)
.setContentTitle("Notification Title")
.setContentText("notification message")
.setNumber(++num)
.setAutoCancel(true).setOnlyAlertOnce(false)
.setTicker("Ding Dong!").setSound(ringtonePrefUri)
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_VIBRATE);
這是所有的代碼。我只是想根據Android版本設置正確的通知圖標。這看起來很簡單,但我還沒有找到一個簡單明瞭的解釋。請幫忙!