2011-06-15 34 views
14

我需要使用更長的文本創建通知,這可能嗎?默認情況下不是,但你可以使用custom layout,這正是我所做的。現在我可以顯示多行,但正如您所看到的那樣,文本仍然被打破/未完全顯示? ):有人可以告訴我我做錯了什麼/如果通知的大小有固定的限制嗎?如果你看截圖,你會注意到,仍然有很多空間......感謝任何提示!Android多行通知/包含更長文本的通知

BTW這裏是用於自定義佈局的XML,基於http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomNotification

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="3dp" 
      > 
<ImageView android:id="@+id/image" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_marginRight="10dp" 
      /> 
<TextView android:id="@+id/text" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:textColor="#000" 
      /> 
</LinearLayout> 

Longer notification text

+0

嘗試使用'layout_height = 「WRAP_CONTENT」'所有你的意見。 – Felix 2011-06-15 12:13:55

+0

謝謝,但這並沒有改變任何東西): – 2011-06-15 12:17:25

+0

據我所知,你不能通知任何大於屏幕截圖中顯示的默認大小,但我可能是錯的。 – 2011-06-15 12:17:26

回答

1

我的理解是,Android的通知系統具有每個通知有限的高度,以避免單個通知填充在屏幕上。

從你的鏈接頁面:

注意:當您使用自定義通知的佈局,要特別小心,以確保您的自定義佈局可與不同的設備取向和分辨率。雖然此建議適用於所有View佈局,但對通知尤其重要,因爲通知抽屜中的空間爲非常受限制。不要讓你的自定義佈局太複雜,並且一定要以各種配置進行測試。

但是,您可以在通知中顯示多個通知,「粘滯」通知或滾動文本。

有關可以使用通知做什麼的更多信息,請參見:

NotificationNotification Builder

+0

我假設你和你一樣,但我仍然在尋找這種行爲的「官方來源」。嗯滾動文本,這是一個想法:-)將查看此,謝謝! – 2011-06-15 12:18:47

+0

添加源:) – Andre 2011-06-15 12:21:20

+1

請注意'Notification.Builder'僅適用於Android 3.0及更高版本。 – Felix 2011-06-15 12:22:29

5

通知觀點得到65sp高度限制。這是實施細節,沒有記錄和has been changed in Android 4.1 to support expandable notifications。所以,不要依賴這個具體的價值,而應該依賴於觀點高度有限的事實。

這裏是status_bar_latest_event.xml這是用於膨脹的觀點在通知區域:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="65sp" 
    android:orientation="vertical" 
    > 

    <com.android.server.status.LatestItemView android:id="@+id/content" 
      android:layout_width="match_parent" 
      android:layout_height="64sp" 
      android:background="@drawable/status_bar_item_background" 
      android:focusable="true" 
      android:clickable="true" 
      android:paddingRight="6sp" 
      > 
    </com.android.server.status.LatestItemView> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="1sp" 
     android:background="@drawable/divider_horizontal_bright" 
     /> 

</LinearLayout> 
+0

不是我不會相信你,但你有一個官方(Android文檔)的源代碼?只是想知道我在哪裏可以看到。謝謝! – 2011-06-15 12:24:25

+0

更新了答案。 – inazaruk 2011-06-15 12:26:05

+0

謝謝你的來源,太棒了! (來源,而不是它是有限的事實:P) – 2011-06-15 12:34:04

1

的Android提供他們所支持3個款式,bigpicture風格,樣式的收件箱,大文本樣式(256 DP)大視圖可擴展的通知,但只能從超過果凍豆的Android版本開始。對於較低版本,我們沒有任何大的文本樣式通知。

36

對於Jelly Bean及更高版本,您可以使用可擴展通知。最簡單的方法是使用NotificationCompat.BigTextStyle作爲通知。

像這樣:

NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(); 
bigTextStyle.setBigContentTitle(getString(R.string.title)); 
bigTextStyle.bigText(getString(R.string.long_explanation)); 

mBuilder.setStyle(bigTextStyle); 
1

這是爲我工作在5.0 它很好地包裹長的行。它還可以讓你提供一系列字符串,這些字符串將以新行分隔顯示。

 String[] multilineDescription = new String[] { "line 1", "another very long line that will get wrapped." }; 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(appContext) 
       .setSmallIcon(smallIcon) 
       .setContentTitle(title) 
       .setContentText("Pull down for more information"); 

     String description; 
     if (null == multilineDescription || multilineDescription.length == 0) { 
      description = "No more information."; 
     } else { 
      description = multilineDescription[0]; 

      for (int i=1; i<multilineDescription.length; i++) { 
       description += System.getProperty("line.separator"); 
       description += multilineDescription[i]; 
      } 
     } 

     builder.setStyle(new NotificationCompat.BigTextStyle().bigText(description)); 
14
NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.ic_notification) 
        .setContentTitle(title) 
        .setStyle(new NotificationCompat.BigTextStyle() 
             .bigText(message)) 
        .setContentText(message)            
        .setDefaults(NotificationCompat.DEFAULT_SOUND) 
        .setContentIntent(contentIntent) 
        .setAutoCancel(true); 

mNotificationManager.notify(requestID, mBuilder.build()); 

一次reffer https://developer.android.com/guide/topics/ui/notifiers/notifications.html

+0

非常感謝! – DmitryKanunnikoff 2017-06-08 15:42:15