Android 7.0 三方APP獲取系統(tǒng)默認(rèn)通知布局顏色的位置

很多App為了適配不同手機(jī)的通知欄配色而讓自己定義的通知欄內(nèi)容顯示清楚使用了不同的自適應(yīng)方案对雪。一般是通過獲取系統(tǒng)默認(rèn)的通知的標(biāo)題和內(nèi)容顏色做文章念链。網(wǎng)上有很多獲取的方法,但是對(duì)于不熟悉系統(tǒng)層的人有時(shí)候不清楚這個(gè)顏色到底是在哪里設(shè)置的,所以來一探究竟硅确。

首先觀察Notification.Builder用來創(chuàng)建通知視圖的方法。

//\frameworks\base\core\java\android\app\Notification.java
        /**
         * Construct a RemoteViews for the final 1U notification layout. In order:
         *   1. Custom contentView from the caller
         *   2. Style's proposed content view
         *   3. Standard template view
         */
        public RemoteViews createContentView() {
            if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
                return mN.contentView;
            } else if (mStyle != null) {
                final RemoteViews styleView = mStyle.makeContentView();
                if (styleView != null) {
                    return styleView;
                }
            }
            // 若沒有自定義的布局和style明肮,則使用標(biāo)準(zhǔn)模板菱农,也就是默認(rèn)布局
            return applyStandardTemplate(getBaseLayoutResource());
        }

然后在找到加載系統(tǒng)默認(rèn)通知布局的方法,發(fā)現(xiàn)其布局文件為notification_template_material_base.xml柿估。

//\frameworks\base\core\java\android\app\Notification.java
        private int getBaseLayoutResource() {
            return R.layout.notification_template_material_base;
        }

于是我們找到這個(gè)布局文件循未,并在其中找到顯示通知內(nèi)容的布局notification_template_part_line1.xml。

<!--\frameworks\base\core\res\res\layout\notification_template_material_base.xml-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/status_bar_latest_event_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:tag="base"
    >
    <include layout="@layout/notification_template_header" />
    <LinearLayout
        android:id="@+id/notification_main_column"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_marginStart="@dimen/notification_content_margin_start"
        android:layout_marginEnd="@dimen/notification_content_margin_end"
        android:layout_marginTop="@dimen/notification_content_margin_top"
        android:layout_marginBottom="@dimen/notification_content_margin_bottom"
        android:orientation="vertical"
        >
       <!-- 顯示通知內(nèi)容的布局  -->
        <include layout="@layout/notification_template_part_line1" />
        <include layout="@layout/notification_template_text" />
    </LinearLayout>
    ...
</FrameLayout>

所以繼續(xù)查看notification_template_part_line1.xml秫舌。

<!--\frameworks\base\core\res\res\layout\notification_template_part_line1.xml-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/line1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <!-- 默認(rèn)通知標(biāo)題 -->
    <TextView android:id="@+id/title"
        android:textAppearance="@style/TextAppearance.Material.Notification.Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        />
    <!-- 默認(rèn)通知內(nèi)容 -->
    <TextView android:id="@+id/text_line_1"
        android:textAppearance="@style/TextAppearance.Material.Notification"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end|bottom"
        android:layout_marginStart="16dp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        />
</LinearLayout>

于是我們發(fā)現(xiàn)的妖,控制默認(rèn)通知標(biāo)題和內(nèi)容的style就是TextAppearance.Material.Notification.Title和TextAppearance.Material.Notification。那么就來看看這兩style足陨,他們?cè)赾olors_material.xml文件中被定義羔味。

<!--\frameworks\base\core\res\res\values\colors_material.xml-->
    <style name="TextAppearance.Material.Notification">
        <item name="textColor">@color/secondary_text_material_light</item>
        <item name="textSize">@dimen/notification_text_size</item>
    </style>

    <style name="TextAppearance.Material.Notification.Title">
        <item name="textColor">@color/primary_text_default_material_light</item>
        <item name="textSize">@dimen/notification_title_text_size</item>
    </style>

到這里我們就可以知道默認(rèn)通知的標(biāo)題和內(nèi)容的顏色分別是secondary_text_material_light和primary_text_default_material_light。因?yàn)檫@是系統(tǒng)根據(jù)自己背景默認(rèn)設(shè)置的顏色钠右,所以在不同的系統(tǒng)上都會(huì)與背景有很大的反差赋元,會(huì)顯示的清楚。因此飒房,根據(jù)這兩個(gè)顏色我們就可以為自己的通知設(shè)置自適應(yīng)的顏色而不會(huì)由于顏色相近顯示不出搁凸。但具體自適應(yīng)的值會(huì)根據(jù)不同的算法而不同。

最后列出系統(tǒng)默認(rèn)通知背景顏色是哪個(gè)值控制狠毯。

<!--\frameworks\base\core\res\res\values\colors.xml-->
    <color name="notification_material_background_color">#ffffff</color>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末护糖,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子嚼松,更是在濱河造成了極大的恐慌嫡良,老刑警劉巖锰扶,帶你破解...
    沈念sama閱讀 216,402評(píng)論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異寝受,居然都是意外死亡坷牛,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門很澄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來京闰,“玉大人,你說我怎么就攤上這事甩苛□彘梗” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵讯蒲,是天一觀的道長(zhǎng)痊土。 經(jīng)常有香客問我,道長(zhǎng)墨林,這世上最難降的妖魔是什么赁酝? 我笑而不...
    開封第一講書人閱讀 58,165評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮萌丈,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘雷则。我一直安慰自己辆雾,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評(píng)論 6 388
  • 文/花漫 我一把揭開白布月劈。 她就那樣靜靜地躺著度迂,像睡著了一般。 火紅的嫁衣襯著肌膚如雪猜揪。 梳的紋絲不亂的頭發(fā)上惭墓,一...
    開封第一講書人閱讀 51,146評(píng)論 1 297
  • 那天,我揣著相機(jī)與錄音而姐,去河邊找鬼腊凶。 笑死,一個(gè)胖子當(dāng)著我的面吹牛拴念,可吹牛的內(nèi)容都是我干的钧萍。 我是一名探鬼主播,決...
    沈念sama閱讀 40,032評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼政鼠,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼风瘦!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起公般,我...
    開封第一講書人閱讀 38,896評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤万搔,失蹤者是張志新(化名)和其女友劉穎胡桨,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體瞬雹,經(jīng)...
    沈念sama閱讀 45,311評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡昧谊,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了挖炬。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片揽浙。...
    茶點(diǎn)故事閱讀 39,696評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖意敛,靈堂內(nèi)的尸體忽然破棺而出馅巷,到底是詐尸還是另有隱情,我是刑警寧澤草姻,帶...
    沈念sama閱讀 35,413評(píng)論 5 343
  • 正文 年R本政府宣布钓猬,位于F島的核電站,受9級(jí)特大地震影響撩独,放射性物質(zhì)發(fā)生泄漏敞曹。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評(píng)論 3 325
  • 文/蒙蒙 一综膀、第九天 我趴在偏房一處隱蔽的房頂上張望澳迫。 院中可真熱鬧,春花似錦剧劝、人聲如沸橄登。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)拢锹。三九已至,卻和暖如春萄喳,著一層夾襖步出監(jiān)牢的瞬間卒稳,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工他巨, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留充坑,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,698評(píng)論 2 368
  • 正文 我出身青樓染突,卻偏偏與公主長(zhǎng)得像匪傍,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子觉痛,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評(píng)論 2 353