当前位置: 首页 > 知识库问答 >
问题:

自定义通知布局:从包发布的错误通知无法展开RemoteViews

葛宏爽
2023-03-14

当我尝试为通知使用一些自定义布局时,我遇到了一个令人作呕的例外。我已经裁剪了图像和2个文本视图的通知,以方便解决方案的查找,但它仍然不会工作。所有类似问题的建议都于事无补(((

这是广播接收机的代码:

public class ReminderOccurredReceiver extends BroadcastReceiver {
    public ReminderOccurredReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        PreferencesWrapper mPrefs = PreferencesWrapper.getWrapper(context);
        boolean uses12HourClock = mPrefs.getIfUse12HourClock();
        Uri soundUri = Uri.parse(mPrefs.getSoundUri());

        String serializedReminder = intent.getStringExtra("reminder");
        Reminder reminder = new Gson().fromJson(serializedReminder, Reminder.class);

        Intent editIntent = new Intent(context, EditReminderActivity.class);
        editIntent.putExtra("serializedReminder", serializedReminder);
        PendingIntent pEditIntent = PendingIntent.getActivity(context, reminder.getDbId(), editIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews notificationView = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
        notificationView.setImageViewResource(R.id.notif_image, R.drawable.ic_launcher);
        notificationView.setTextViewText(R.id.notif_message, reminder.getMessage());
        notificationView.setTextViewText(R.id.notif_time, reminder.getFormattedTZTime(uses12HourClock));

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker(reminder.getMessage())
            .setContent(notificationView)
            .setContentIntent(pEditIntent);
//            .setSound(soundUri)

        NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        nManager.notify(reminder.getDbId(), builder.build());
    }
}
<?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="@dimen/notification_height"
    android:padding="3dp"
    >

    <ImageView android:id="@+id/notif_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        />

    <TextView android:id="@+id/notif_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/notif_image"
        android:textSize="17sp"
        android:singleLine="true"
        />

    <TextView android:id="@+id/notif_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/notif_image"
        android:layout_alignParentBottom="true"
        />
</RelativeLayout>

在Nexus4@4.4.4上测试

共有1个答案

司空奕
2023-03-14

崩溃的原因是在固定高度的通知布局提供,我记得。只有match_parent可用。我敢打赌,这甚至在某个地方的文件上都有说明,而我只是忽略了它。

 类似资料: