這裏有一些奇怪的行爲。我有一堆代碼,圖片,然後是一些奇怪行爲的描述,然後,然後我的問題。Android自定義通知未顯示代碼更改
文件
我有一個文件random_start_bonus_cards_notification.xml
,它看起來像:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/firstCard" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/secondCard" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ifNotCoal"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="("
android:paddingTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/if_not_coal"
android:paddingTop="15dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/thirdCard" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=")"
android:paddingTop="15dp" />
</LinearLayout>
和文件random_start_bonus_cards.xml
<?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="match_parent"></RelativeLayout>
這是一個故意BLA nk佈局,因爲我們會考慮到奇怪。事實證明,這個文件必須在這裏,但它包含的內容並不重要。
Annnnd我styles.xml
:
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
</style>
<style name="NotificationTitle">
<item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
<item name="android:textStyle">bold</item>
</style>
<style name="NotificationText">
<item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
</style>
和一些Android的代碼:
private void showStartBonusCardsDialog(DrawnCards drawnCards) {
LayoutInflater factory = LayoutInflater.from(CalculatorActivity.this);
final View dialogContent = factory.inflate(R.layout.random_start_bonus_cards_notification, null);
((ImageView) dialogContent.findViewById(R.id.firstCard)).setImageResource(getDrawableIdForStartCard(drawnCards.firstCard()));
((ImageView) dialogContent.findViewById(R.id.secondCard)).setImageResource(getDrawableIdForStartCard(drawnCards.secondCard()));
if(drawnCards.hasThirdCard()) {
dialogContent.findViewById(R.id.ifNotCoal).setVisibility(View.VISIBLE);
((ImageView) dialogContent.findViewById(R.id.thirdCard)).setImageResource(getDrawableIdForStartCard(drawnCards.thirdCard()));
} else {
dialogContent.findViewById(R.id.ifNotCoal).setVisibility(View.GONE);
}
AlertDialog drawnCardsDialog = new AlertDialog.Builder(CalculatorActivity.this).create();
drawnCardsDialog.setView(dialogContent);
drawnCardsDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); }
});
drawnCardsDialog.show();
}
private void showStartBonusCardsNotification(DrawnCards drawnCards) {
Intent openIntent = new Intent(CalculatorActivity.this, CalculatorActivity.class);
openIntent.setAction(refreshStartCardsAction);
PendingIntent openPendingIntent = PendingIntent.getActivity(this, 0, openIntent, 0);
RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.random_start_bonus_cards_notification);
notificationView.setImageViewResource(R.id.firstCard, getDrawableIdForStartCard(drawnCards.firstCard()));
notificationView.setImageViewResource(R.id.secondCard, getDrawableIdForStartCard(drawnCards.secondCard()));
if(drawnCards.hasThirdCard()) {
notificationView.setViewVisibility(R.id.ifNotCoal, View.VISIBLE);
notificationView.setImageViewResource(R.id.thirdCard, getDrawableIdForStartCard(drawnCards.thirdCard()));
} else {
notificationView.setViewVisibility(R.id.ifNotCoal, View.GONE);
}
NotificationCompat.Builder notification = new NotificationCompat.Builder(this);
notification.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
notification.setSmallIcon(R.drawable.white);
notification.setContentIntent(openPendingIntent);
notification.setContent(notificationView);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = 1;
notificationManager.notify(notificationId, notification.build());
}
古怪
我最初只有一個佈局文件,因爲佈局是相同的。有了這個,對話框的文字顏色是黑色的(好),通知的文字顏色是白色的,無法在我的HTC M9上看到。 (1)如果我將來自random_start_bonus_cards_notification.xml
的XML寫入random_start_bonus_cards.xml
,並將random_start_bonus_cards_notification
的所有用法替換爲random_start_bonus_cards
,則文本顏色是錯誤的。如果我按原樣保留代碼,但刪除random_start_bonus_cards
,則文本顏色錯誤。如果我在項目中使用random_start_bonus_cards_notification.xml
並保留random_start_bonus_cards.xml
,即使它爲空,我在對話框和通知中都會顯示黑色文本!
(2)通知的背景是淡綠色。此顏色在某個時間點位於資源文件中,但已被刪除(如圖所示)。刪除背景顏色後,對話框佈局具有白色背景,但通知背景仍爲淡綠色,無論我是否將其更改爲紅色或任何其他顏色。
(3)如果我停止調用對話框代碼,則會發生相同的行爲。我已經包含了對話框代碼,因爲我擔心這裏可能存在一些交互,但我不知道如何。
問題
一般情況下,如何RemoteViews與自定義通知的佈局工作?有什麼樣的系統緩存可以解釋背景顏色不變嗎?
爲什麼我更改佈局背景顏色時不改變背景顏色?
爲什麼當我刪除這個空文件random_start_bonus_cards.xml
時,我的應用程序改變了行爲,這甚至沒有被使用?
爲什麼我的通知文字顏色不起作用,除非我使用名爲random_start_bonus_cards_notification.xml
的文件? (如果我刪除了_notification
,文字將更改顏色)。
感謝您的閱讀!
不幸的是,這些都不是的原因,即我可以看到。我把一些東西放回去了,現在我的通知是一個淺灰色,對話框是黑色文本。它是可讀的,但並不完美,我想要它。不管怎麼說,還是要謝謝你。 – rythos42
我確認沒有清理項目,沒有對正在運行的應用程序進行更改。我想知道增量構建是否被破壞? 我最終使_和_notifications除了樣式相同,向通知添加了一些樣式,並使用@android:color/primary_text_light作爲文本顏色。這與這些東西的所有流行智慧背道而馳。 但是這是一個愛好項目,所以有人可以抱怨,如果它不適合他們......> – rythos42
你試過調試顯示通知的邏輯嗎?文本顏色由'textColorPrimaryInverse'的值控制,因此請檢查strings.xml文件中的值。必須有一些邏輯破壞構建,並在您每次更新代碼時強制您進行清理。 –