简体   繁体   English

Android,在theme.xml文件中设置工具栏样式

[英]Android, set Toolbar style in theme.xml file

I am trying to customize the look of Toolbar in my app.我正在尝试在我的应用程序中自定义工具栏的外观。

Target目标
I would like to set the title color and the background color of the Toolbar.我想设置工具栏的标题颜色和背景颜色。
I read about how Theme and Style works, so I was able to try some solutions.我阅读了有关主题和样式的工作原理,因此我能够尝试一些解决方案。 I found out that title color is managed by app:titleTextColor property and the background color from the android:background attribute.我发现标题颜色由app:titleTextColor属性和android:background属性中的背景颜色管理。


Premise前提
To know how to act it is necessary to know how the style of the widget that we need to modify is managed.要知道如何操作,有必要知道我们需要修改的小部件的样式是如何管理的。

The base Toolbar style is defined in the Widget.MaterialComponents.Toolbar style as below.基本工具栏样式在Widget.MaterialComponents.Toolbar样式中定义,如下所示。

<style name="Widget.MaterialComponents.Toolbar" parent="Widget.AppCompat.Toolbar">
    <item name="titleTextAppearance">?attr/textAppearanceHeadline6</item>
    <item name="titleTextColor">?android:attr/textColorPrimary</item>
    <item name="subtitleTextAppearance">?attr/textAppearanceSubtitle1</item>
    <item name="subtitleTextColor">?android:attr/textColorSecondary</item>
    <!-- Overrides minimum height in landscape to avoid headline6 and subtitle1 height concerns. -->
    <item name="android:minHeight">@dimen/mtrl_toolbar_default_height</item>
    <item name="maxButtonHeight">@dimen/mtrl_toolbar_default_height</item>
</style>

Here the value of titleTextColor is set to ?android:attr/textColorPrimary .这里titleTextColor的值设置为?android:attr/textColorPrimary


So my first attempt was simply to add the ovverride of android:textColorPrimary attribute in my app theme.所以我的第一次尝试只是在我的应用程序主题中添加android:textColorPrimary属性的 ovverride。

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_500</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>

    <!-- Customize your theme here. -->
    <item name="android:textColorPrimary">@color/white</item>
</style>

Result: This works.结果:这行得通。

To also set the background I decided to create a custom Toolbar style where to put the background definition, so I changed my theme as below.为了同时设置背景,我决定创建一个自定义工具栏样式来放置背景定义,所以我改变了我的主题,如下所示。

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    ...    
    <!-- Customize your theme here. -->
    <item name="toolbarStyle">@style/MyToolbarStyle</item>
</style>
<style name="MyToolbarStyle" parent="Widget.MaterialComponents.Toolbar">
    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:background">?attr/colorPrimary</item>
</style>

Result: This does not works.结果:这不起作用。 The android:background is applied but the android:textColorPrimary not.应用了android:background ,但没有应用android:textColorPrimary

Setting the value of titleTextColor directly inside MyToolbarStyle works.直接在MyToolbarStyle中设置titleTextColor的值是可行的。

<style name="MyToolbarStyle" parent="Widget.MaterialComponents.Toolbar">
    <item name="titleTextColor">?attr/colorOnPrimary</item>
    <item name="android:background">?attr/colorPrimary</item>
</style>

I also tried to set the toolbar theme where I override the Android attribute:textColorPrimary , as below.我还尝试设置工具栏主题,其中我覆盖了 Android attribute:textColorPrimary ,如下所示。

<style name="Base_ToolbarStyle" parent="Widget.MaterialComponents.Toolbar">
    <item name="android:background">?attr/colorPrimary</item>
    <item name="android:theme">@style/Base_ToolbarTheme</item>
</style>

<style name="Base_ToolbarTheme">
    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:textColorSecondary">@color/white</item>
</style>

Result: This does not works too.结果:这也不起作用。

Now I am a bit confused from the outcome of my tests.现在我对我的测试结果有点困惑。

Why is it not possible to override the android:textColorPrimary in the custom style?为什么无法覆盖自定义样式中的android:textColorPrimary

To apply a style you can use (and the toolbarStyle attribute applies this style to all the Toolbar ) the style attribute:要应用style ,您可以使用(并且toolbarStyle属性将此样式应用于所有Toolbarstyle属性:

   <androidx.appcompat.widget.Toolbar
         style="@style/MyToolbarStyle"

with:和:

<style name="MyToolbarStyle" parent="Widget.MaterialComponents.Toolbar">
    <item name="titleTextColor">?attr/colorOnPrimary</item>
    <item name="android:background">?attr/colorPrimary</item>
</style>

because titleTextColor is a property defined in the Toolbar .因为titleTextColorToolbar中定义的属性。

To apply a theme overlay you can use the android:theme attribute:要应用主题覆盖,您可以使用android:theme属性:

   <androidx.appcompat.widget.Toolbar
        android:theme="@style/MyToolbarThemeOverlay"

with

<style name="MyToolbarThemeOverlay">
    <item name="android:textColorPrimary">@color/white</item>
</style>

because android:textColorPrimary is an attribute defined at theme level.因为android:textColorPrimary是在主题级别定义的属性。

If you want to override a theme attribute directly in a style you have to use the materialThemeOverlay attribute:如果要直接在样式中覆盖主题属性,则必须使用materialThemeOverlay属性:

<style name="MyToolbarStyle" parent="Widget.MaterialComponents.Toolbar">
    <item name="android:background">?attr/colorPrimary</item>
    <item name="materialThemeOverlay">@style/MyToolbarThemeOverlay</item>
</style>

With using the AppCompat Material Toolbar androidx.appcompat.widget.Toolbar , I have made the following definitions to customize the Toolbar in a theme使用 AppCompat 材质工具栏androidx.appcompat.widget.Toolbar ,我做了以下定义来自定义主题中的工具栏

Given this toolbar in a layout file:给定布局文件中的此工具栏:

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/MyAppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        app:popupTheme="@style/MyAppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>

Define a toolbar style in themes.xml :themes.xml中定义工具栏样式:

<style name="MyAppTheme.Toolbar" parent="Widget.MaterialComponents.Toolbar">
    <!-- Add custom stuff here -->
    <item name="android:background" tools:targetApi="l">@color/dark_primary</item>
    <item name="background">@color/dark_primary</item>
</style>

And also add the overlay styles in themes.xml :并且还在themes.xml中添加覆盖themes.xml

<style name="MyAppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="MyAppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

And then apply in app theme:然后在应用主题中应用:

<style name="MyAppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    ...
    <item name="android:toolbarStyle" tools:targetApi="l">@style/MyAppTheme.Toolbar</item>
    <item name="toolbarStyle">@style/MyAppTheme.Toolbar</item>
    ...
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM