这里有一些奇怪的行为。我有一堆代码和图片,接着是一些关于奇怪行为的描述,然后是我的问题。
文件
我有一个名为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](https://istack.dev59.com/J3iiF.webp)
![Notification](https://istack.dev59.com/hdWfZ.webp)
怪异现象
我最初只有一个布局文件,因为这些布局是相同的。由此,对话框的文本颜色是黑色(不错),而通知的文本颜色是白色的,在我的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
,则文本颜色会更改)。
谢谢阅读!
textColorPrimaryInverse
的值控制,因此请检查strings.xml文件中的值。一定有一些逻辑导致构建失败,并在每次更新代码时强制进行清理。 - Michele La Ferla