在Android应用程序主题中如何指定工具栏主题?

9

目前我正在使用Material组件和Material主题:

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

我可以使用materialButtonStyle为每个按钮定义样式,我想知道是否可能为工具栏实现相同的效果。

这很重要:想法是避免在appbar或toolbar组件中定义样式/主题,而只需使用它获取应用程序主题定义的样式。

我想避免这种情况:

<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>

在主题中,我希望能够指定字体样式和文本外观。
这是我的样式:
  <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>

使用之前定义的代码,我能够获取那些更改。但正如我所提到的,我希望像材料按钮一样将其定义为我的应用程序主题的一部分。

我尝试了这个方法,但没有成功:

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

11

这段代码对我来说完全有效:

<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>

我已经成功地使用你的代码实现了工具栏样式。但是,我仍然在应用主题到应用栏方面遇到问题。据我所知,您需要应用一个背景才能使其正常工作。但是,如果您在appBar中使用app:theme,则不需要使用背景颜色,这也是使用ThemeOverlay.MaterialComponents.Dark.ActionBar的想法吧。 - Leandro Ocampo

2
您可以在应用主题中添加toolbarStyle属性以定制工具栏的样式:
<style name="BaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="toolbarStyle">@style/MyToolbar</item>
</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>

在你的布局中,请使用 com.google.android.material.appbar.MaterialToolbar
要为 AppBarLayout 全局定义样式,请在应用程序主题中使用属性(在您的情况下不清楚您想要自定义哪些内容)。
<item name="appBarLayoutStyle">@style/...</item>

谢谢!你有没有想法如何在应用程序主题中指定appBarTheme?例如...<style name="AppBarTheme" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar"> - Leandro Ocampo
@LeandroOcampo 更新了回答。但是不清楚你想要实现什么。也许有更好的方法。 - Gabriele Mariotti

1
在样式文件中添加以下内容:

     <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>

然后在你的MainActivity类中:
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
     setSupportActionBar(toolbar); 
     toolbar.setTitleTextAppearance(this, R.style.MyToolbarStyle);

你好!感谢你的回答。问题是这与通过xml设置样式相同。我想在应用程序主题中指定它。 - Leandro Ocampo
除了编程实现,其他方法都对我无效。对此造成的任何不便,敬请谅解。 - private static

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接