国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Notification自定義界面

2019-12-12 01:35:38
字體:
來源:轉載
供稿:網友

前言

之前在做一個手機的播放器,需要做到在通知欄顯示控制播放的界面,如下:

這里寫圖片描述

這是讓服務在前臺運行就可以實現的(可以參考我的前一篇文章Service在前臺運行),今天我們就要實現Notification的自定義界面,當然就不實現如上圖所示的了,而是下面一個簡單的界面,隨自己的需要搭建自己想要的界面。

這里寫圖片描述

可以看到,我實現了一個簡單的界面,包括一個ImageView和Button,下面我就說說該如何實現它,其實很簡單。

實現

首先我們要準備一個界面文件:

notification.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:background="#333300" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView  android:paddingLeft="20dp"  android:layout_width="70dp"  android:layout_height="50dp"  android:src="@drawable/ic_qiuda"  /> <Button  android:layout_marginLeft="30dp"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="點擊我"  /></LinearLayout>

然后新建一個Service的子類,MyService:

public class MyService extends Service { public static final String TAG = "MyService"; @Override public void onCreate() {  super.onCreate();  Notification notification = new Notification(R.drawable.ic_launcher,    "JcMan", System.currentTimeMillis());  RemoteViews view = new RemoteViews(getPackageName(),R.layout.notification);  notification.contentView = view;  startForeground(1, notification); } @Override public IBinder onBind(Intent intent) {  return null; }}

可以看到,在onCreate方法里面我們設置界面的不再是用LayoutInflater來得到界面,而是用RemoteViews來new出來一個界面,構造方法傳入的是包名和界面資源的ID即可,然后我們把notification.contentView設置成我們new出來的自定義界面即可。

小結

普通的Notification可以用來進行通知,但是當有特殊需要的時候,我們就需要自定義界面,而且有時候還需要對自定義的界面添加點擊的方法,如在上圖的界面里面有一個Button如何對Button的點擊事件進行響應,這是一個比較難的問題,因為這不是簡單的setOnClickListener就可以的,需要另外的實現,需要用到廣播機制,我將會在下一篇文章中說明如何為Notification的自定義界面添加點擊事件。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 开远市| 斗六市| 福鼎市| 峨眉山市| 盘锦市| 永寿县| 赫章县| 仙桃市| 突泉县| 莱芜市| 申扎县| 得荣县| 荔浦县| 乌拉特中旗| 旬邑县| 兰溪市| 建瓯市| 普兰县| 开封县| 天水市| 治多县| 博爱县| 黄大仙区| 剑阁县| 沙河市| 淮阳县| 黄陵县| 水富县| 三江| 句容市| 三门县| 金湖县| 普陀区| 昌邑市| 樟树市| 理塘县| 北流市| 蓬安县| 万山特区| 沛县| 潍坊市|