2

在我的Android应用程序中,我想创建一个消耗性通知。为此,我必须设置一个样式像这样的通知:创建自己的NotificationStyle

NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 

builder.setSmallIcon(android.R.drawable.ic_menu_info_details); 
builder.setContentTitle("My notification"); 
builder.setContentText("Hello World!"); 

builder.setStyle(...); 

的Android给了我一些风格,我可以用这个方法使用,但我喜欢创造我自己,所以我可以加载在通知的布局。那我该怎么做?如果我创建的Style一个子类,只有这两种方法:

public Notification build(); 
public void setBuilder(Builder builder); 

那么,怎样才能在我的通知加载我自己的布局? builder.setContent对我来说是不够的,因为那时我只有64dp的版面。

回答

0

找到了解决这个问题的方法。这很容易。请在此处使用此代码:

String packageName = this.getPackageName(); 
RemoteViews bigView = new RemoteViews(packageName, R.layout.test); 

NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
builder.setSmallIcon(android.R.drawable.ic_menu_info_details); 
builder.setContentTitle("My notification"); 
builder.setContentText("Hello World!"); 

Notification noti = builder.build(); 
noti.bigContentView = bigView; 

NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
mgr.notify(1234, noti);