很多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>