2016-07-26 26 views
1

我想創建一個通知,爲我的應用程序具有通常的圖標,標題和潛臺詞,但也有右側的操作按鈕通知無需擴展成大模式。文字的線條總是很短,所以這個按鈕有足夠的空間。目前,它看起來像這樣Android通知 - 在普通視圖通知(沒有擴大到大視圖)上的動作按鈕

1

但是,當它被另一個通知壓縮到普通視圖,它看起來像這樣,失去按鈕

2

我與自定義視圖RemoteViews,但問題是我不想手動設置圖標和文本的位置和樣式。在多個設備上讓系統繼續照顧它們會更加可靠。

是否可以將我自己的按鈕添加到通知出現在第二張圖片的右側?有點像將遠程視圖的一個元素與Android生成的標準佈局組合在一起?從我讀過的內容來看,我認爲在普通視圖中使用動作按鈕是不可能的?

我希望能夠做到這一點的原因是允許用戶點擊鬧鐘以確認他們已經看到它,以便它不會熄滅,但他們仍然有即將發生的事件的通知。點擊除鬧鐘按鈕以外的任何地方應該像平常一樣打開應用程序。

代碼明智,它看起來像這樣,與遠程視圖註釋掉。

RemoteViews remoteViews = new RemoteViews(activity.getPackageName(), 
       R.layout.custom_notification); 

     String eventDescription = preferences.getString(activity.getResources().getString(R.string.next_event_description), "Freemasons run the country"); 
     String eventTime = preferences.getString(activity.getResources().getString(R.string.next_event_time), "00:00"); 

    NotificationManager notificationManager = (NotificationManager) 
    activity.getSystemService(activity.NOTIFICATION_SERVICE); 

    Intent intent = new Intent(activity, MainActivity.class); 
    PendingIntent pIntent = PendingIntent.getActivity(activity, 0 , intent, 0); 

    Notification.Action action = new Notification.Action.Builder(
      Icon.createWithResource(activity, R.mipmap.alarm_icon), 
      nextEventAlarmMinutes.toString(), 
      pIntent).build(); 

    Notification.Builder builder = new Notification.Builder(activity) 
      //.setContent(remoteViews) 
      .setContentTitle(eventDescription) 
      .setContentText(eventTime) 
      .setSmallIcon(R.mipmap.alarm_icon) 
      .setContentIntent(pIntent) 
      .addAction(action) 
      //.setPriority(Notification.PRIORITY_HIGH) 
      .setWhen(0) 
      .setVisibility(Notification.VISIBILITY_PUBLIC) 
      .setAutoCancel(false); 

     notificationManager.notify(0, builder.build()); 

自定義視圖XML只是有一個相對佈局和按鈕

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 

<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:padding="10dp" /> 

是否有可能,相對佈局覆蓋了整個事情,我可以只使用按鈕沒有一個父視圖持有它自己的觀點?

+0

問題,而不是使用URL – Jas

+0

@馬特你能請張貼相關的代碼的一部分嵌入你的形象? – Leo

回答

0
//Share intent 
Intent shareAction = new Intent(this, ActShare.class); 
PendingIntent pendingIntentShare = PendingIntent.getActivity(this, 12345, shareAction, PendingIntent.FLAG_UPDATE_CURRENT); 
mBuilder.addAction(R.drawable.share, "Share", pendingIntentShare); 

//Editintent 
Intent EditAction = new Intent(this, ActEdit.class); 
PendingIntent pendingIntentEdit = PendingIntent.getActivity(this, 12345, EditAction, PendingIntent.FLAG_UPDATE_CURRENT); 
mBuilder.addAction(R.drawable.edit, "Edit", pendingIntentEdit); 

//Share intent 
Intent deleteAction = new Intent(this, ActDelete.class); 
PendingIntent pendingIntentDelete = PendingIntent.getActivity(this, 12345, deleteAction, PendingIntent.FLAG_UPDATE_CURRENT); 
mBuilder.addAction(R.drawable.delete, "Delete", pendingIntentDelete); 

添加該代碼的addAction在默認的Android通知..

+0

對不起,我不明白這是如何回答這個問題呢?這不僅僅是將三種不同的操作添加到通知中,並且仍然要求它處於大模式下? – Matt