亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Notification自定義界面

 更新時(shí)間:2017年11月30日 08:46:41   作者:潘建成  
這篇文章主要為大家詳細(xì)介紹了Notification自定義界面的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

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

這里寫圖片描述

這是讓服務(wù)在前臺(tái)運(yùn)行就可以實(shí)現(xiàn)的(可以參考我的前一篇文章Service在前臺(tái)運(yùn)行),今天我們就要實(shí)現(xiàn)Notification的自定義界面,當(dāng)然就不實(shí)現(xiàn)如上圖所示的了,而是下面一個(gè)簡單的界面,隨自己的需要搭建自己想要的界面。

這里寫圖片描述

可以看到,我實(shí)現(xiàn)了一個(gè)簡單的界面,包括一個(gè)ImageView和Button,下面我就說說該如何實(shí)現(xiàn)它,其實(shí)很簡單。

實(shí)現(xiàn)

首先我們要準(zhǔn)備一個(gè)界面文件:

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="點(diǎn)擊我"
  />
</LinearLayout>

然后新建一個(gè)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方法里面我們?cè)O(shè)置界面的不再是用LayoutInflater來得到界面,而是用RemoteViews來new出來一個(gè)界面,構(gòu)造方法傳入的是包名和界面資源的ID即可,然后我們把notification.contentView設(shè)置成我們new出來的自定義界面即可。

小結(jié)

普通的Notification可以用來進(jìn)行通知,但是當(dāng)有特殊需要的時(shí)候,我們就需要自定義界面,而且有時(shí)候還需要對(duì)自定義的界面添加點(diǎn)擊的方法,如在上圖的界面里面有一個(gè)Button如何對(duì)Button的點(diǎn)擊事件進(jìn)行響應(yīng),這是一個(gè)比較難的問題,因?yàn)檫@不是簡單的setOnClickListener就可以的,需要另外的實(shí)現(xiàn),需要用到廣播機(jī)制,我將會(huì)在下一篇文章中說明如何為Notification的自定義界面添加點(diǎn)擊事件。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論