带有 RemoteViews 的 Android 自定义通知布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23222063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android custom notification layout with RemoteViews
提问by HappyRave
I'm trying to create a custom notification for my android app using this post, and I stumbled upon a problem which I tried to resolve for the last 2 hours.
我正在尝试使用这篇文章为我的 android 应用程序创建一个自定义通知,但我偶然发现了一个我在过去 2 小时内试图解决的问题。
Only the imageview of my layout shows, and I can't figure out how to make it works like intended.
仅显示我的布局的图像视图,我无法弄清楚如何使其按预期工作。
My code:
我的代码:
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);
}
}
The xml file:
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>


Thanks for the help
谢谢您的帮助
回答by Libin
Problem is you have not set the correct contentView.
问题是您没有设置正确的 contentView。
mBuilder.setContent(contentView);
But your remote view is mContentView. change it to
但是您的远程视图是 mContentView。将其更改为
mBuilder.setContent(mContentView);
Here is a sample
这是一个示例
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());
回答by Artem Kleinschmidt
You can see working example here: http://www.laurivan.com/android-notifications-with-custom-layout/
您可以在此处查看工作示例:http: //www.laurivan.com/android-notifications-with-custom-layout/
I think, adding
我认为,添加
// Set Icon
.setSmallIcon(R.drawable.smallimage)
// Set Ticker Message
.setTicker("Noification is created");
to your builder can solve the problem.
给你的builder可以解决问题。
回答by Douglas Mesquita
Thispost explains very cool.
这篇文章解释的很酷。
Basically and so:
基本上如此:
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);
回答by Travis
I think this is just a layout issue, the more that I think about it. I think that if you were to set android:layout_alignParentRight="true"on the TextViews, you'd get what you were looking for. It looks like it's laying them out to the right of the ImageView, but not giving them any width at layout time, since they don't ask for any, and they don't have any content.
我认为这只是一个布局问题,我想得越多。我认为如果你android:layout_alignParentRight="true"在TextViews上设置,你会得到你想要的。看起来它把它们放在 的右侧ImageView,但在布局时没有给它们任何宽度,因为它们不要求任何宽度,并且它们没有任何内容。
If that doesn't work, let us know!
如果这不起作用,请告诉我们!

