2016-07-19 61 views
14

這裏有一些奇怪的行爲。我有一堆代碼,圖片,然後是一些奇怪行爲的描述,然後,然後我的問題。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()); 
} 

而這裏的兩個截圖指: Dialog Notification

古怪

我最初只有一個佈局文件,因爲佈局是相同的。有了這個,對話框的文字顏色是黑色的(好),通知的文字顏色是白色的,無法在我的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,文字將更改顏色)。

感謝您的閱讀!

回答

3

這是令人發狂的,但仍然不是很好。 (1)在確認我的手機正在做一些緩存之後,我開始使用模擬器,它運行得更好。 (2)即使它們是相同的,我仍然在對話框中留下了單獨的佈局文件的通知。我的推理是,顯然通知背景可以在Android版本之間改變(有很多投訴)。

(3)我沒有發佈它,但有一次我有一個「v9/styles.xml」。我沒有意識到這適用於全部以上版本的API9。

最終的解決方案看起來有點像下面的代碼。我從這個SO回答(5.1.1 notification TextView, the correct way to deal with white text?)複製並使用textAppearance而不是樣式。

 <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/if_not_coal" 
     android:textAppearance="@style/NotificationText" 
     android:paddingTop="15dp" /> 

textAppearance是巨大的。

<style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" /> 

給人的文本< API淺灰色顏色21.

<style name="NotificationText" parent="@android:style/TextAppearance.Material.Notification.Title"></style> 

賦予文字黑色爲API> = 21

6

我的猜測是,這是happenning出於以下兩個原因:

  1. 其中第一個可以在你的代碼別的地方,你指的創建的通知,並你所做的更改不會影響代碼。

  2. 這更可能發生。構建已經崩潰,您的應用程序的內置版本不使用最新的代碼。您可以通過從IDE重建應用程序來解決此問題。

如果仍然不能設法得到這是爲什麼happenning的保持,只是打電話來顯示通知,並按照應用程序使用的邏輯方法之前設置一個斷點。

希望這會有所幫助:)

+0

不幸的是,這些都不是的原因,即我可以看到。我把一些東西放回去了,現在我的通知是一個淺灰色,對話框是黑色文本。它是可讀的,但並不完美,我想要它。不管怎麼說,還是要謝謝你。 – rythos42

+0

我確認沒有清理項目,沒有對正在運行的應用程序進行更改。我想知道增量構建是否被破壞? 我最終使_和_notifications除了樣式相同,向通知添加了一些樣式,並使用@android:color/primary_text_light作爲文本顏色。這與這些東西的所有流行智慧背道而馳。 但是這是一個愛好項目,所以有人可以抱怨,如果它不適合他們......> – rythos42

+0

你試過調試顯示通知的邏輯嗎?文本顏色由'textColorPrimaryInverse'的值控制,因此請檢查strings.xml文件中的值。必須有一些邏輯破壞構建,並在您每次更新代碼時強制您進行清理。 –