Android自定义通知RemotView不使用样式

时间:2016-01-26 10:27:08

标签: java android xml android-layout android-notifications

我尝试使用可以展开的远程视图创建自定义通知,并具有不同的TextView和按钮。这很好但布局中的大多数字段都不使用样式。 花了4个多小时进行搜索并尝试修复它之后,我发现谷歌代码中唯一一个描述此错误的问题:

的https:// code.google.com / P /机器人显影剂预览/问题/细节ID = 1894

有谁知道如何解决这个问题或绕过这个问题?

我尝试使用我的样式或Android样式,没有任何样式...... 看起来它取决于该字段的位置,因为在expaned版本中,第一个字段接受该样式。

我的测试代码

创建通知

       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>

enter image description here

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>

enter image description here

我用于测试的样式

<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>

1 个答案:

答案 0 :(得分:0)

花了几个小时之后,我发现了错误......

如果您通过xml提示字段提供TextView的文本:

    android:hint="Test Field 1"

并且不要通过

覆盖或设置文本
        remoteViews.setTextViewText(R.id.title, "TEXT");

然后是在选中的xml中,样式未应用且文本保持白色。

因为我正在使用虚拟文本来创建布局,所以我很早就没有认识到这一点......