Android自定义通知未显示代码更改

15

这里有一些奇怪的行为。我有一堆代码和图片,接着是一些关于奇怪行为的描述,然后是我的问题。

文件

我有一个名为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>

这是一个故意留空的布局,因为我们将要涉及到一些奇怪的事情。事实证明,这个文件必须存在,但它所包含的内容并不重要。

还有我的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.xml并保留random_start_bonus_cards_notification.xml,即使它为空,我在对话框和通知中都得到黑色文本!

(2) 通知的背景是淡绿色。此颜色曾经在资源文件中,但已被删除(如图所示)。删除背景颜色后,对话框布局具有白色背景,但通知背景仍然是淡绿色,无论我将其更改为红色或任何颜色。

(3) 如果我停止调用对话框代码,则会出现相同的行为。我包含了对话框代码,因为我担心可能存在某种交互作用,但我不知道是怎么回事。

问题

RemoteViews如何与自定义通知布局一起使用?是否有任何系统缓存可以解释背景颜色未更改的原因?

为什么当我更改布局背景颜色时,我的通知背景颜色没有更改?

为什么当我删除甚至未被使用的空文件random_start_bonus_cards.xml时,我的应用程序会更改行为?

为什么只有使用名为random_start_bonus_cards_notification.xml的文件时,我的通知文本颜色才有效?(如果我删除_notification,则文本颜色会更改)。

谢谢阅读!

2个回答

6
我的猜测是出现这种情况有以下两个原因之一:
  1. 第一个可能是你的代码的其他地方引用了通知的创建,而你所做的更改不会影响到那段代码。

  2. 这种情况更有可能发生。构建已经崩溃,你构建的应用程序版本没有使用最新的代码。你可以通过从IDE中重新构建应用程序来解决这个问题。

如果你仍然无法弄清楚为什么会出现这种情况,请在调用显示通知的方法之前设置断点,并跟踪应用程序正在使用的逻辑。

希望能对你有所帮助 :)


不幸的是,这两个原因都不是我能看到的原因。我把一些东西放回去,现在我的通知是浅灰色,而对话框是黑色文本。它是可读的,但不是我想要的完美状态。无论如何,还是谢谢。 - rythos42
1
我确认了一下,如果不清理项目,则运行的应用程序不会发生任何更改。我想知道增量构建是否出现问题了?最终,我让_make_和_notifications_除了样式外完全相同,为通知添加了一些样式,并使用@android:color/primary_text_light作为文本颜色。这与所有关于这方面的主流智慧都不符。但这只是一个业余项目,所以如果对某些人不起作用,他们可以抱怨...>.> - rythos42
你尝试过调试显示通知的逻辑吗?文本颜色由textColorPrimaryInverse的值控制,因此请检查strings.xml文件中的值。一定有一些逻辑导致构建失败,并在每次更新代码时强制进行清理。 - Michele La Ferla
感谢您的关注!我找到了一个不错的解决方案,并在下面发布了相关内容。 - rythos42

3

这真是令人恼火,而且现在还不太好。

(1) 确认我的手机正在进行一些缓存后,我开始使用模拟器,效果更好。

(2) 我将通知与对话框分开放置,即使它们完全相同。我的想法是,显然通知背景可能会在 Android 版本之间发生变化(有很多投诉)。

(3) 我上面没有发布过,但我曾经有一个 "v9/styles.xml"。我没有意识到这适用于 API9 以上的 所有 版本。

最终的解决方案看起来有点像下面的代码。我从这篇 Stack Overflow 回答中复制了代码(5.1.1 notification TextView, the correct way to deal with white text?),并使用了 textAppearance 而不是 style。

        <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中,将文本设置为黑色。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接