LoginSignup
3
3

More than 3 years have passed since last update.

Toolbarのタイトルやアイコンにテーマの属性のcolorOnSurfaceで色がつかないときの対処法

Last updated at Posted at 2019-11-17

最近のMaterialComponentではテーマに以下のように設定しておけばPrimaryColorの上の文字が自動でこのattributeになるはずでしたが、若干ハマったのでメモとして残しておきます。

<item name="colorOnPrimary">@color/white</item>

でもToolbarでsetSupportActionBar(binding.toolbar)していても、ちゃんとテーマが適応されていても、タイトルや色が変わりませんでした。

サンプルを見に行くと以下のようにstyleを設定する必要があるようです。
https://github.com/material-components/material-components-android/blob/d2870fbb3fd170712abfb6d6ff8bccf799360f5b/catalog/java/io/material/catalog/topappbar/res/layout/cat_topappbar_toolbar_fragment.xml#L40

<com.google.android.material.appbar.MaterialToolbar
  style="@style/Widget.MaterialComponents.Toolbar.Primary"

このstyleを見に行くと以下のように設定するように書かれています。

  </style>
    <style name="Widget.MaterialComponents.Toolbar.Primary">
    <item name="android:elevation" ns2:ignore="NewApi">@dimen/design_appbar_elevation</item>
    <item name="android:background">?attr/colorPrimary</item>
    ****
    <item name="titleTextColor">?attr/colorOnPrimary</item> 
    ****
    <item name="subtitleTextColor">@color/material_on_primary_emphasis_medium</item>
    <!-- Note: this theme overlay will only work if the style is applied directly to a Toolbar. -->
    <item name="android:theme">@style/ThemeOverlay.MaterialComponents.Toolbar.Primary</item>
  </style>

Widget.MaterialComponents.Toolbar.Surfaceというstyleもありますが、これはattr/colorSurfaceをバックグラウンドとして使いたいときに使うようです。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3