使用RemoteViews自定义Android通知布局

11

我正在尝试使用这个帖子为我的安卓应用程序创建自定义通知,但我遇到了一个问题,我已经试图解决它已经有两个小时了。

只有我的布局中的ImageView显示出来了,我无法弄清楚如何使其按照预期工作。

我的代码:

package be.ac.ucl.lfsab1509.cumulus.services;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.widget.RemoteViews;
import be.ac.ucl.lfsab1509.cumulus.R;
import be.ac.ucl.lfsab1509.cumulus.activities.MainActivity;
import be.ac.ucl.lfsab1509.cumulus.entities.Song;

public class CumulusNotification {

private CumulusService mCtx;
private CumulusMediaPlayer mPlayer;
private Notification mNotification;
private NotificationCompat.Builder mBuilder;
private RemoteViews mContentView;

public CumulusNotification(CumulusService ctx, CumulusMediaPlayer player){
    mCtx = ctx;
    mPlayer = player;

    mBuilder = new NotificationCompat.Builder(
            mCtx);
    mBuilder.setSmallIcon(R.drawable.ic_stat_cumulus_notification);
    //mBuilder.setContentTitle("Cumulus is playing");
    //mBuilder.setTicker("Cumulus");
    //mBuilder.setContentText(mCtx.getCurrentSong().title());

    Intent resultIntent = new Intent(mCtx, MainActivity.class);
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    resultIntent.setAction(Intent.ACTION_MAIN);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(mCtx);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    mContentView = new RemoteViews(mCtx.getPackageName(), R.layout.statusbar);
    mContentView.setImageViewResource(R.id.notifimage, R.drawable.cumulus_icon);
    mContentView.setTextViewText(R.id.notiftitle, "Custom notification");
    mContentView.setTextViewText(R.id.notiftext, "This is a custom layout");
    mBuilder.setContent(mContentView);
}

public void startNotification() {
    mNotification = mBuilder.build();
    //mNotification.contentView = mContentView;
    mCtx.startForeground(R.integer.NOTIFICATION_ID, mNotification);
}

public void updateSong(Song song){
    mBuilder.setContentText(song.title());
    mNotification = mBuilder.build();
    mCtx.startForeground(R.integer.NOTIFICATION_ID, mNotification);
}
}

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:gravity="left" >
<ImageView android:id="@+id/notifimage"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="10dp" />
<TextView android:id="@+id/notiftitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/notifimage" />
<TextView android:id="@+id/notiftext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/notifimage"
    android:layout_below="@id/notiftitle" />
</RelativeLayout>

我的通知

感谢帮助


如果您发布statusbar.xml文件,将会更容易帮助您修复布局问题... - Travis
完成!就像我在原帖中所说的那样。 - HappyRave
@HappyRave - 你解决了吗?我也遇到了同样的问题。谢谢。 - Amit
为什么两个TextView都有android:layout_toRightOf="@id/notifimage" - IgorGanapolsky
4个回答

13

问题在于您没有设置正确的 contentView。

mBuilder.setContent(contentView);

但是你的远程视图是mContentView。将其更改为

mBuilder.setContent(mContentView);

这是一个样本

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher);

    RemoteViews mContentView = new RemoteViews(getPackageName(), R.layout.test_tt);
    mContentView.setImageViewResource(R.id.image, R.drawable.ic_action_search);
    mContentView.setTextViewText(R.id.title, "Custom notification");
    mContentView.setTextViewText(R.id.text, "This is a custom layout");
    mBuilder.setContent(mContentView);

    startForeground(1001, mBuilder.build());

1
不是这样的,这只是在 stack overflow 上的一个打字错误!如果这是我的实际代码,它甚至都无法编译 ^^ - HappyRave
此外,还要移除你为TextView设置的样式。如果需要,它应该是一个有效的样式。 - Libin

2

0

这篇文章解释得非常棒。

基本上就是这样:

RemoteViews downloadViews = new RemoteViews(context.getPackageName(), R.layout.download_notification);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Content Title")
                .setContentText("Content Text")
                .setContent(downloadViews)
                .setPriority(NotificationCompat.PRIORITY_MIN);
        final Notification notification = mBuilder.build();
        notification.bigContentView = downloadViews;

        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, notification);

0

我认为这只是一个布局问题,我越想越觉得是这样。如果你在 TextView 上设置 android:layout_alignParentRight="true",你会得到你想要的效果。看起来它们被布局到了 ImageView 的右侧,但在布局时没有给它们任何宽度,因为它们没有请求任何宽度,也没有任何内容。

如果这不起作用,请告诉我们!


现在整个Java文件可用 :) - HappyRave

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