我曾经使用actionBarStyle
更改AppCompat状态栏颜色,并创建一个背景为我想要的颜色的样式。
现在,使用Material Design AppCompat,此方法不再适用。
你能帮帮我吗?感谢。答案 0 :(得分:90)
您可以在主题中定义名为colorPrimary
的新属性。这将为ActionBar或工具栏提供纯色。
举了一个例子:
<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>
请注意:除了colorPrimary
之外的每个值文件夹中,它必须只有android:colorPrimary
,而不是values-v21
。
您可以在developer.android.com上阅读有关自定义调色板的更多信息。
答案 1 :(得分:3)
就我而言,@ reVerse得到了部分答案。我必须进行一些额外的更改才能使colorPrimary正常工作,因为我正在自定义工具栏的其他部分......特别是文本颜色。
这是我的工作styles.xml文件,其中的注释表明我做错了什么:
<!-- 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。
为了完整性,这里是我用于工具栏的工具栏文件的布局文件:
<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" />
希望这有助于某人!
答案 2 :(得分:0)
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/ColorPrimary</item>
<item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
<!-- Customize your theme here. -->
</style>
答案 3 :(得分:0)
设置应用栏的directions in the documentation建议在Android Manifest文件中设置主题,如下所示:
<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可确保文字清晰。)
答案 4 :(得分:0)
<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>