Android 自定义通知 RemotView 不使用样式
Android Custom notification RemotView uses no Styles
我尝试创建一个带有远程视图的自定义通知,该视图可以展开并具有不同的 TextView 和按钮。这很好用,但布局中的大多数字段不使用样式。
在花了 4 个多小时搜索并尝试修复它之后,我唯一发现的是 google 代码中的一个问题,它描述了这个错误:
https://code.google.com/p/android-developer-preview/issues/detail?id=1894
有没有人知道如何解决或绕过这个问题?
我尝试使用我的样式或 android 样式,但没有任何样式...
看起来它取决于字段的位置,因为在扩展版本中,第一个字段接受样式。
我的测试代码
创建通知
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// set the view layout
RemoteViews remoteViewsNotification = new RemoteViews(this.getPackageName(),
R.layout.notification_non_expanded);
remoteViewsNotification.setTextViewText(R.id.notification_test_field_1, "non-expanded");
RemoteViews remoteViewsExpandedNotification = new RemoteViews(this.getPackageName(),
R.layout.notification_expanded);
remoteViewsExpandedNotification.setTextViewText(R.id.notification_test_field_1, "expanded");
// create a new notification intent and set the actions
Intent notificationIntent = new Intent(this, TrainingInProgress.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// Create the notification and set all values
Notification notification = new NotificationCompat.Builder(this)
.setContent(remoteViewsNotification)
.setSmallIcon(sportsIconId)
.setContentIntent(pendingIntent)
.setOngoing(true).build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification.bigContentView = remoteViewsExpandedNotification;
}
notificationManager.notify(notifyId, notification);
XML notification_non_expanded
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/notification_test_field_1"
style="@style/TextAppearance.StatusBar.EventContent.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="Test Field 1" />
<TextView
android:id="@+id/notification_test_field_2"
android:layout_below="@id/notification_test_field_1"
style="@style/TextAppearance.StatusBar.EventContent.Line2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="Test Field 2" />
<TextView
android:id="@+id/notification_test_field_3"
style="@style/TextAppearance.StatusBar.EventContent.Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="Test Field 3"
android:layout_below="@+id/notification_test_field_1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="@+id/notification_test_field_4"
style="@style/TextAppearance.StatusBar.EventContent.Info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="Test Field 4"
android:layout_below="@+id/notification_test_field_2"
android:layout_centerInParent="true" />
</RelativeLayout>
XML notification_expanded
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/notification_test_field_1"
style="@style/mainHeadlineBigTextStyleDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="Test Field 1" />
<TextView
android:id="@+id/notification_test_field_2"
android:layout_below="@id/notification_test_field_1"
style="@style/mainHeadlineBigTextStyleDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="Test Field 2" />
<TextView
android:id="@+id/notification_test_field_3"
style="@style/mainHeadlineBigTextStyleDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="Test Field 3"
android:layout_below="@+id/notification_test_field_1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="@+id/notification_test_field_4"
style="@style/mainHeadlineBigTextStyleDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="Test Field 4"
android:layout_below="@+id/notification_test_field_2"
android:layout_centerInParent="true" />
</RelativeLayout>
我用于测试的样式
<style name="mainHeadlineBigTextStyleDark">
<item name="android:gravity">center</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">20sp</item>
<item name="android:textColor">@color/mainHeadlineBigTextColorDark</item>
</style>
<color name="mainHeadlineBigTextColorDark">#747474</color>
又花了几个小时,我发现了错误...
如果您通过 xml 提示字段为 TextView 提供文本:
android:hint="Test Field 1"
并且不要通过
覆盖或设置文本
remoteViews.setTextViewText(R.id.title, "TEXT");
然后是,在 xml 选中,未应用样式,文本保持白色。
因为我正在使用虚拟文本来创建布局,所以我没有这么早意识到...
我尝试创建一个带有远程视图的自定义通知,该视图可以展开并具有不同的 TextView 和按钮。这很好用,但布局中的大多数字段不使用样式。 在花了 4 个多小时搜索并尝试修复它之后,我唯一发现的是 google 代码中的一个问题,它描述了这个错误:
https://code.google.com/p/android-developer-preview/issues/detail?id=1894
有没有人知道如何解决或绕过这个问题?
我尝试使用我的样式或 android 样式,但没有任何样式... 看起来它取决于字段的位置,因为在扩展版本中,第一个字段接受样式。
我的测试代码
创建通知
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// set the view layout
RemoteViews remoteViewsNotification = new RemoteViews(this.getPackageName(),
R.layout.notification_non_expanded);
remoteViewsNotification.setTextViewText(R.id.notification_test_field_1, "non-expanded");
RemoteViews remoteViewsExpandedNotification = new RemoteViews(this.getPackageName(),
R.layout.notification_expanded);
remoteViewsExpandedNotification.setTextViewText(R.id.notification_test_field_1, "expanded");
// create a new notification intent and set the actions
Intent notificationIntent = new Intent(this, TrainingInProgress.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// Create the notification and set all values
Notification notification = new NotificationCompat.Builder(this)
.setContent(remoteViewsNotification)
.setSmallIcon(sportsIconId)
.setContentIntent(pendingIntent)
.setOngoing(true).build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification.bigContentView = remoteViewsExpandedNotification;
}
notificationManager.notify(notifyId, notification);
XML notification_non_expanded
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/notification_test_field_1"
style="@style/TextAppearance.StatusBar.EventContent.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="Test Field 1" />
<TextView
android:id="@+id/notification_test_field_2"
android:layout_below="@id/notification_test_field_1"
style="@style/TextAppearance.StatusBar.EventContent.Line2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="Test Field 2" />
<TextView
android:id="@+id/notification_test_field_3"
style="@style/TextAppearance.StatusBar.EventContent.Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="Test Field 3"
android:layout_below="@+id/notification_test_field_1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="@+id/notification_test_field_4"
style="@style/TextAppearance.StatusBar.EventContent.Info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="Test Field 4"
android:layout_below="@+id/notification_test_field_2"
android:layout_centerInParent="true" />
</RelativeLayout>
XML notification_expanded
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/notification_test_field_1"
style="@style/mainHeadlineBigTextStyleDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="Test Field 1" />
<TextView
android:id="@+id/notification_test_field_2"
android:layout_below="@id/notification_test_field_1"
style="@style/mainHeadlineBigTextStyleDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="Test Field 2" />
<TextView
android:id="@+id/notification_test_field_3"
style="@style/mainHeadlineBigTextStyleDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="Test Field 3"
android:layout_below="@+id/notification_test_field_1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="@+id/notification_test_field_4"
style="@style/mainHeadlineBigTextStyleDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:hint="Test Field 4"
android:layout_below="@+id/notification_test_field_2"
android:layout_centerInParent="true" />
</RelativeLayout>
我用于测试的样式
<style name="mainHeadlineBigTextStyleDark">
<item name="android:gravity">center</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">20sp</item>
<item name="android:textColor">@color/mainHeadlineBigTextColorDark</item>
</style>
<color name="mainHeadlineBigTextColorDark">#747474</color>
又花了几个小时,我发现了错误...
如果您通过 xml 提示字段为 TextView 提供文本:
android:hint="Test Field 1"
并且不要通过
覆盖或设置文本 remoteViews.setTextViewText(R.id.title, "TEXT");
然后是,在 xml 选中,未应用样式,文本保持白色。
因为我正在使用虚拟文本来创建布局,所以我没有这么早意识到...