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