Android中使用Notification實現寬通知欄(Notification樣本二)

來源:互聯網
上載者:User

標籤:pre   gif   展開   sum   builder   new   prot   自己的   組成   

Notification是在你的應用常規介面之外展示的訊息。當app讓系統發送一個訊息的時候,訊息首先以圖表的形式顯示在通知欄。要查看訊息的詳情需要進入通知抽屜(notificationdrawer)中查看。通知欄和通知抽屜

(notificationdrawer)都是系統層面控制的,你可以隨時查看,不限制於app。

Notification 的設計:

作為android UI中很重要的組成部分,notification擁有專屬於自己的設計準則。

Notification的介面元素在通知抽屜中的notification有兩種顯示方式,取決於你的android版本以及notification drawer的狀態:

(1)普通視圖

這種風格是notification drawer的標準顯示方式。

普通視圖博文連結:http://www.cnblogs.com/panhouye/p/6139386.html

(2)寬視圖

指你的notification被展開的時候會顯示更大的視圖,這種風格是android4.1之後才有的新特性。

下面將詳細介紹寬視圖:

只有當notification被展開的時候這種寬視圖的notification才會出現,通過手勢操作可以展開一個普通的notification(部分定製系統不可以,會直接顯示寬視圖)。這種風格的notification從android4.1以後才開始支援。下面的展示了inbox風格的notification:

你應該注意到了這種notification其實跟普通的沒多大差別,

唯一的區別在於數字7-詳情地區。

不同寬視圖notification這裡的顯示是有區別的,有如下幾種風格:

大表徵圖風格:詳情地區顯示一個最高位256dp的bitmap。

文字風格:詳情地區顯示一段文字

訊息盒子風格(Inbox style):詳情地區顯示幾行文字。

 下面是我們本次示範的:

下面開始代碼實現,這裡就不贅述點擊按鈕的布置了,直接上java的notification實現代碼:

 1 public class MainActivity extends AppCompatActivity { 2     private static final int NO_2 =0x2; 3     @Override 4     protected void onCreate(Bundle savedInstanceState) { 5         super.onCreate(savedInstanceState); 6         setContentView(R.layout.activity_main); 7     } 8     //按鈕的點擊方法 9     public  void show(View v){10         NotificationCompat.Builder builder = new NotificationCompat.Builder(this);11         builder.setSmallIcon(R.mipmap.guojia);12         builder.setContentTitle("郭嘉");//系統限制,可能不顯示13         builder.setContentText("我們打袁紹吧");//系統限制,可能不顯示14         builder.setDefaults(Notification.DEFAULT_ALL);15         //添加寬視圖16         NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();17         style.setBigContentTitle("十勝十敗");18         //由手機螢幕像素決定顯示多少19         style.addLine("第一是道勝");20         style.addLine("第二是義勝");21         style.addLine("第三是治勝");22         style.addLine("第四是度勝");23         style.addLine("第五是謀勝");24         style.addLine("第六是德勝");25         style.addLine("第七是仁勝");26         style.addLine("第八是明勝");27         style.addLine("第九是文勝");28         style.addLine("第十是武勝");29         style.setSummaryText("郭嘉");//添加概要30         builder.setStyle(style);31         Notification n = builder.build();32         NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);33         manager.notify(NO_2, n);34     }35 }

今天就這樣,簡單示範了notification的寬視圖的實現,大家睡前看看,晚安了

Android中使用Notification實現寬通知欄(Notification樣本二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.