安卓仿網(wǎng)易云音樂通知欄控制音樂力试,默認(rèn)顯示Notification bigView

最近在做一個音樂播放器的時候遇到了一個關(guān)于notification的問題焚虱,在網(wǎng)上找了很久都沒有頭緒。后來找到了解決的辦法懂版,特意記錄一下。

問題描述

首先請看網(wǎng)易云音樂的通知欄


普通高度的notification.png

加高notification.png

可以看到躏率,網(wǎng)易云音樂的通知欄有兩種躯畴,一種是普通的通知欄,一種是顯示bigView的通知欄薇芝。這兩種通知欄是可以通過雙指上滑/下滑來切換的(說實話,我今天才知道原來還有這個功能...)蓬抄。

網(wǎng)易云音樂在播放音樂的時候,默認(rèn)顯示的是第二種通知欄夯到,而我今天遇到的問題是:通過自定義布局來實現(xiàn)通知欄嚷缭,默認(rèn)情況下只能顯示第一種普通高度的通知欄,需要雙指滑動一下才能顯示bigView通知欄耍贾。

對于大多數(shù)用戶來說阅爽,可能并不知道有雙指滑動通知欄這一功能,因此這樣的用戶體驗是不夠友好的荐开,那么付翁,如何實現(xiàn)像網(wǎng)易云一樣的,默認(rèn)就顯示bigView的notification呢晃听?

首先新建一個測試項目百侧,再新建兩個布局,一個用作普通的notification視圖能扒,一個用作bigView的notification視圖:

  1. 普通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>
  1. 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)啟動的時候:

默認(rèn)啟動時顯示的通知欄

雙指滑動后效果:
2.png

PS:
1.上面寫的兩個布局文件沒有參考意義黄琼,快下班了,噼里啪啦寫出來的整慎,估計有些問題
2.有空錄下GIF脏款,效果應(yīng)該會更直觀

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市裤园,隨后出現(xiàn)的幾起案子撤师,更是在濱河造成了極大的恐慌,老刑警劉巖拧揽,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件剃盾,死亡現(xiàn)場離奇詭異,居然都是意外死亡淤袜,警方通過查閱死者的電腦和手機痒谴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來铡羡,“玉大人闰歪,你說我怎么就攤上這事”褪” “怎么了库倘?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長论矾。 經(jīng)常有香客問我教翩,道長,這世上最難降的妖魔是什么贪壳? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任饱亿,我火速辦了婚禮,結(jié)果婚禮上闰靴,老公的妹妹穿的比我還像新娘彪笼。我一直安慰自己,他們只是感情好蚂且,可當(dāng)我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布配猫。 她就那樣靜靜地躺著,像睡著了一般杏死。 火紅的嫁衣襯著肌膚如雪泵肄。 梳的紋絲不亂的頭發(fā)上捆交,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機與錄音腐巢,去河邊找鬼品追。 笑死,一個胖子當(dāng)著我的面吹牛冯丙,可吹牛的內(nèi)容都是我干的肉瓦。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼胃惜,長吁一口氣:“原來是場噩夢啊……” “哼泞莉!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蛹疯,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎热监,沒想到半個月后捺弦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡孝扛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年列吼,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片苦始。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡寞钥,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出陌选,到底是詐尸還是另有隱情理郑,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布咨油,位于F島的核電站您炉,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏役电。R本人自食惡果不足惜赚爵,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望法瑟。 院中可真熱鬧冀膝,春花似錦、人聲如沸霎挟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽酥夭。三九已至枯芬,卻和暖如春论笔,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背千所。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工狂魔, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人淫痰。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓最楷,卻偏偏與公主長得像,于是被迫代替她去往敵國和親待错。 傳聞我的和親對象是個殘疾皇子籽孙,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,724評論 2 354