最近在做一個音樂播放器的時候遇到了一個關(guān)于notification的問題焚虱,在網(wǎng)上找了很久都沒有頭緒。后來找到了解決的辦法懂版,特意記錄一下。
問題描述
首先請看網(wǎng)易云音樂的通知欄
可以看到躏率,網(wǎng)易云音樂的通知欄有兩種躯畴,一種是普通的通知欄,一種是顯示bigView的通知欄薇芝。這兩種通知欄是可以通過雙指上滑/下滑來切換的(說實話,我今天才知道原來還有這個功能...)蓬抄。
網(wǎng)易云音樂在播放音樂的時候,默認(rèn)顯示的是第二種通知欄夯到,而我今天遇到的問題是:通過自定義布局來實現(xiàn)通知欄嚷缭,默認(rèn)情況下只能顯示第一種普通高度的通知欄,需要雙指滑動一下才能顯示bigView通知欄耍贾。
對于大多數(shù)用戶來說阅爽,可能并不知道有雙指滑動通知欄這一功能,因此這樣的用戶體驗是不夠友好的荐开,那么付翁,如何實現(xiàn)像網(wǎng)易云一樣的,默認(rèn)就顯示bigView的notification呢晃听?
首先新建一個測試項目百侧,再新建兩個布局,一個用作普通的notification視圖能扒,一個用作bigView的notification視圖:
- 普通notification視圖佣渴,命名為normal_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@mipmap/icon"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="64dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="歌曲名"
android:textSize="18sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="歌手名"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="64dp"
android:gravity="center_vertical">
<ImageButton
android:layout_width="26dp"
android:layout_height="26dp"
android:background="@null"
android:scaleType="fitCenter"
android:src="@mipmap/pre"/>
<ImageButton
android:layout_marginStart="10dp"
android:layout_width="26dp"
android:layout_height="26dp"
android:background="@null"
android:scaleType="fitCenter"
android:src="@mipmap/play"/>
<ImageButton
android:layout_marginStart="10dp"
android:layout_width="26dp"
android:layout_height="26dp"
android:background="@null"
android:scaleType="fitCenter"
android:src="@mipmap/next"/>
</LinearLayout>
</LinearLayout>
- bigView的notification視圖,命名為big_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="110dp"
android:layout_height="110dp"
android:src="@mipmap/icon"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="110dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_song_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="歌名"
android:textSize="15sp"/>
<TextView
android:id="@+id/tv_song_singer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:text="歌名"
android:textSize="12sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageButton
android:layout_marginEnd="10dp"
android:layout_width="26dp"
android:layout_height="26dp"
android:background="@null"
android:scaleType="fitCenter"
android:src="@mipmap/like"/>
<ImageButton
android:layout_marginEnd="10dp"
android:layout_width="26dp"
android:layout_height="26dp"
android:background="@null"
android:scaleType="fitCenter"
android:src="@mipmap/pre"/>
<ImageButton
android:layout_marginEnd="10dp"
android:layout_width="38dp"
android:layout_height="38dp"
android:background="@null"
android:scaleType="fitCenter"
android:src="@mipmap/play"/>
<ImageButton
android:layout_marginEnd="10dp"
android:layout_width="26dp"
android:layout_height="26dp"
android:background="@null"
android:scaleType="fitCenter"
android:src="@mipmap/next"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
除了這兩個用作notification的布局文件之外初斑,在主界面中還有一個按鈕辛润,通過按鈕的點擊事件,模擬開始播放音樂见秤,發(fā)送通知频蛔。這里就不貼代碼了,直接貼按鈕點擊事件中的核心代碼:
//發(fā)送自定義視圖通知
public void sendCustomViewNotification(View view) {
//普通notification用到的視圖
RemoteViews normalView = new RemoteViews(getPackageName(), R.layout.normal_notification);
//顯示bigView的notification用到的視圖
RemoteViews bigView = new RemoteViews(getPackageName(), R.layout.big_notification);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("開始播放啦~~")
.setOngoing(true)
.setContent(normalView)//設(shè)置普通notification視圖
.setCustomBigContentView(bigView)//設(shè)置顯示bigView的notification視圖
.setPriority(NotificationCompat.PRIORITY_MAX)//設(shè)置最大優(yōu)先級
.build();
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(22, notification);
}
上面這段代碼相信大家應(yīng)該經(jīng)常使用秦叛,這是構(gòu)建一個自定義通知的基本代碼晦溪。在使用builder對象為notification設(shè)置一些列熟悉的時候,有三個關(guān)鍵的方法挣跋,我都已經(jīng)注釋出來了
1.setContent:給notification設(shè)置普通狀態(tài)下顯示的視圖
2. setCustomBigContentView():給notification設(shè)置bigView
設(shè)置完上面兩個方法之后三圆,程序應(yīng)該能實現(xiàn)雙指滑動切換普通notification和bigView notification了,但還無法像網(wǎng)易云的通知欄一樣,默認(rèn)顯示bigView notification舟肉,所以:
3. setPriority(NotificationCompat.PRIORITY_MAX):給notification設(shè)置最高的優(yōu)先級
設(shè)置了優(yōu)先級之后修噪,終于能夠?qū)崿F(xiàn)和網(wǎng)易云音樂相同的通知欄效果了,大功告成路媚,下面上圖:
默認(rèn)啟動的時候:
雙指滑動后效果:
2.png
PS:
1.上面寫的兩個布局文件沒有參考意義黄琼,快下班了,噼里啪啦寫出來的整慎,估計有些問題
2.有空錄下GIF脏款,效果應(yīng)該會更直觀