带有RemoteViews的Android自定义通知布局

我试图创build一个自定义通知我的Android应用程序使用这个职位 ,我偶然发现了一个问题,我试图解决过去2个小时。

只有我的布局的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> 

我的通知

谢谢您的帮助

Solutions Collecting From Web of "带有RemoteViews的Android自定义通知布局"

问题是你没有设置正确的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()); 

你可以在这里看到工作示例: http : //www.laurivan.com/android-notifications-with-custom-layout/

我认为,补充

 // Set Icon .setSmallIcon(R.drawable.smallimage) // Set Ticker Message .setTicker("Noification is created"); 

给你的build设者可以解决问题。

我认为这只是一个布局问题,我想的更多。 我认为,如果你在TextView上设置android:layout_alignParentRight="true" ,你会得到你正在寻找的东西。 它看起来像将它们放在ImageView的右边,但是在布局的时候没有给它们任何宽度,因为它们没有要求任何东西,也没有任何内容。

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

这篇文章解释很酷。

基本上是这样的:

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