简体   繁体   English

如何在 Android 的 App Theme 中指定 Toolbar 主题?

[英]How to specify a Toolbar theme in the App Theme in Android?

Currently I am using material components and material theme:目前我正在使用材质组件和材质主题:

  <style name="BaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
   ...
   ...
   <item name="materialButtonStyle">@style/Button</item>
  </style>

I was able to define a style for every button using materialButtonStyle and I was wondering if it is possible to achieve the same for a toolbar.我能够使用 materialButtonStyle 为每个按钮定义一个样式,我想知道是否可以为工具栏实现相同的样式。

THIS IS REALLY IMPORTANT: The idea is to avoid defining a style / theme in the appbar or toolbar component, but just use it a get the styles defined by the app theme.这非常重要:这个想法是避免在应用程序栏或工具栏组件中定义样式/主题,而只是使用它来获取应用程序主题定义的 styles。

I want to avoid this:我想避免这种情况:

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:theme="@style/ToolbarTheme">

    <com.google.android.material.appbar.MaterialToolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:titleTextAppearance="@style/ToolbarTextAppearance"
      tools:title="Title" />

</com.google.android.material.appbar.AppBarLayout>

Inside the theme I want to be able to specify the font style and the text appearance.在主题内,我希望能够指定字体样式和文本外观。

These are my style:这些是我的风格:

  <style name="ToolbarTheme" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar">
  </style>

  <style name="ToolbarTextAppearance" parent="TextAppearance.AppCompat.Title">
    <item name="android:fontFamily">@font/nunito_bold</item>
  </style>

Using the code defined previously, I am able to get those changes.使用之前定义的代码,我可以获得这些更改。 But as I mentioned, I want to defined that as part of my app theme like the material button.但正如我所提到的,我想将其定义为我的应用程序主题的一部分,例如材质按钮。

I tried using this but I did not succeed:我尝试使用它,但没有成功:

<style name="BaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    ...
    <item name="appBarLayoutStyle">@style/ToolbarTheme</item>
    <item name="toolbarStyle">@style/ToolbarTheme</item>
</style>

This code works for me just fine:这段代码对我来说很好:

<resources>

    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="appBarLayoutStyle">@style/AppTheme.AppBarOverlay</item>
        <item name="toolbarStyle">@style/AppTheme.Toolbar</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar">

        <item name="android:background">?colorPrimary</item>

    </style>

    <style name="AppTheme.Toolbar" parent="Widget.MaterialComponents.Toolbar">

        <item name="titleTextAppearance">@style/TextAppearance.MaterialComponents.Body1</item>

    </style>

</resources>

You can add in your app theme the toolbarStyle attribute:您可以在应用主题中添加toolbarStyle属性:

<style name="BaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="toolbarStyle">@style/MyToolbar</item>
</style>

Then define your custom style:然后定义您的自定义样式:

<style name="MyToolbar" parent="@style/Widget.MaterialComponents.Toolbar">
  <item name="titleTextAppearance">@style/MyToolbartitleTextAppearance</item>
</style>

<style name="MyToolbartitleTextAppearance" parent="@style/TextAppearance.MaterialComponents.Headline6">
   ...
   <item name="fontFamily">....</item>
   <item name="android:fontFamily">....</item>    
</style>

In your layout use the com.google.android.material.appbar.MaterialToolbar .在您的布局中使用com.google.android.material.appbar.MaterialToolbar

To define globally a style for the AppBarLayout use in your app theme the attribute (in your case is not clear what you want to customize).要为AppBarLayout全局定义样式,请在您的应用主题中使用该属性(在您的情况下,不清楚您要自定义什么)。

<item name="appBarLayoutStyle">@style/...</item>

In Styles file add this:在 Styles 文件中添加:

     <style name="MyToolbarStyle" parent="@android:style/TextAppearance.Medium">
       <item name="android:textColor">?attr/color_text_primary</item>
       <item name="android:textSize">20sp</item>
      <item name="android:fontFamily">sans-serif-smallcaps</item>
     </style>

Then in your MainActivity class:然后在您的 MainActivity class 中:

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
     setSupportActionBar(toolbar); 
     toolbar.setTitleTextAppearance(this, R.style.MyToolbarStyle);

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

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