MaterialComponents未指定颜色时随机着色状态栏与Toolbar问题
问题
为何在colors.xml未指定任何颜色时,MaterialComponents会为状态栏和Toolbar随机应用颜色?能否移除日间模式的这些颜色,使其显示白色?我了解MaterialComponents支持更优的主题定制,但无需主次配色方案,此前使用AppCompat时无此问题。
相关代码
使用AppCompat时的主题配置
values/themes.xml & night/themes.xml:
<resources> <style name="Theme.MyApp" parent="Theme.AppCompat.DayNight.NoActionBar"/> </resources>
使用MaterialComponents时的主题配置
<resources> <style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar"/> </resources>
CollapsingToolbarLayout布局代码
<?xml version="1.0" encoding="utf-8"?> <com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/myAppBarLayout"> <com.google.android.material.appbar.CollapsingToolbarLayout android:id="@+id/myCollapsingToolbarLayout" android:layout_width="match_parent" android:layout_height="200dp" android:fitsSystemWindows="true" app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"> <androidx.appcompat.widget.Toolbar android:id="@+id/myToolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:contentInsetStartWithNavigation="0dp" app:layout_collapseMode="pin" /> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout>
主布局代码
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/myCoordinatorLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/collapsing_toolbar" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/myRecyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" android:scrollbarStyle="outsideInset" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>
解决方案
这事儿我之前迁移到Material Components时也踩过坑!其实核心原因是:Material Components主题自带了一套默认的配色生成逻辑,哪怕你没在colors.xml里定义任何颜色,它也会自动生成一套基于默认色值的主题色(比如常见的蓝色系),而AppCompat主题默认就是白色Toolbar+状态栏,所以之前没这个问题。
要让日间模式下Toolbar和状态栏都显示白色,只需要在主题里覆盖几个关键属性就行,不用搞复杂的主次配色方案:
修改日间模式主题(values/themes.xml)
<resources> <style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <!-- 设置Toolbar的主色为白色 --> <item name="colorPrimary">@android:color/white</item> <item name="colorPrimaryVariant">@android:color/white</item> <!-- 设置状态栏颜色为白色 --> <item name="statusBarColor">@android:color/white</item> <!-- 适配Android 5.0+的状态栏颜色 --> <item name="android:statusBarColor">@android:color/white</item> <!-- 白色背景下,把Toolbar的文字/图标设为深色,避免看不见 --> <item name="android:textColorPrimary">@android:color/black</item> <item name="colorOnPrimary">@android:color/black</item> </style> </resources>
额外优化(可选)
如果你的CollapsingToolbarLayout也出现了默认的背景色,可以直接在布局里给它加上白色背景:
<com.google.android.material.appbar.CollapsingToolbarLayout android:id="@+id/myCollapsingToolbarLayout" android:layout_width="match_parent" android:layout_height="200dp" android:fitsSystemWindows="true" android:background="@android:color/white" app:layout_scrollFlags="scroll|snap|exitUntilCollapsed">
这样设置后,日间模式下的Toolbar、状态栏就都是纯白色了,文字和图标也会自动变成深色,完全符合你的需求~夜间模式可以保持night/themes.xml的原有配置,不受影响。
内容的提问来源于stack exchange,提问作者wbk727
相关产品推荐
相关产品推荐

