以前我使用 actionBarStyle
更改 AppCompat 状态栏颜色,创建一个带有所需颜色背景的样式。
现在,使用 Material Design AppCompat,这种方法不再起作用。
您能帮我吗?谢谢。
以前我使用 actionBarStyle
更改 AppCompat 状态栏颜色,创建一个带有所需颜色背景的样式。
现在,使用 Material Design AppCompat,这种方法不再起作用。
您能帮我吗?谢谢。
现在有一个名为colorPrimary
的新属性,您可以在自己的主题中定义它。这将使您的操作栏或工具栏拥有一个纯色。
以下是一个简单的示例:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/my_action_bar_color</item>
</style>
请注意:除了values-v21
文件夹之外的每个值文件夹中,都只能使用colorPrimary
而不是android:colorPrimary
。
您可以在developer.android.com上阅读有关自定义颜色调色板的更多信息。
colorPrimary
是 v7-appcompat
的一部分,因此基本上适用于 API-Level 7 及以上版本(请注意父级 Theme.AppCompat
)。 - reVerseandroid:colorPrimary
,但实际上你只需要colorPrimary
(没有android:
前缀)。再看一下我的帖子。 ;) - reVerse<!-- As @reVerse mentioned, you need to use colorPrimary instead of android:colorPrimary -->
<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/my_toolbar_color</item>
</style>
<!-- I was incorrectly using @style/Theme.AppCompat.Light for the popupTheme attribute -->
<style name="MyToolbarStyle" parent="Widget.AppCompat.ActionBar">
<item name="theme">@style/MyToolbarTextStyle</item>
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
</style>
<!-- I was incorrectly using Them.AppCompat.Light for the parent here -->
<style name="MyToolbarTextStyle" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:textColorPrimary">@color/my_toolbar_text_color</item>
</style>
因此,在自定义工具栏背景颜色时,您需要确保使用ThemeOverlay主题之一,否则由于某种原因,colorPrimary将被忽略。
为了完整起见,这是我用于我的工具栏的布局文件Toolbar.xml:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schmeas.android.com/apk/res-auto"
style="@style/MyToolbarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
希望这能帮助到某些人!
<style name="Theme.EmojiBible" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="actionBarTheme">@style/ThemeOverlay.Actionbar</item>
</style>
<style name="ThemeOverlay.Actionbar" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar" >
<item name="android:textColorPrimary">#f00</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/ColorPrimary</item>
<item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
<!-- Customize your theme here. -->
</style>
另请参阅:制作自定义操作栏
在文档中设置应用栏的指南建议在Android清单文件中像这样设置主题:
<application
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
/>
然而,根据我的经验,这会覆盖为自定义AppTheme设置的colorPrimary
样式。对我有效的方法是定义一个自定义主题,如下所示:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
然后在 AndroidManifest 文件中使用此主题。
<application
android:theme="@style/AppTheme"
/>
之后根据colorPrimary
在活动中设置工具栏背景颜色就可以正常工作了。
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
(使用深色主题设置ThemeOverlay可以确保文本呈现为浅色。)