Как изменить цвет фона ActionBar в новых MaterialComponents

Я использую новые MaterialComponents, и моя тема приложения:

<style name="AppTheme" parent="Theme.MaterialComponents.Light">
    <item name="colorPrimary">@color/primaryColor</item>
    <item name="colorPrimaryDark">@color/primaryDarkColor</item>
    <item name="colorAccent">@color/secondaryColor</item>
</style>

как изменить цвет фона ActionBar

Я знаю об этом вопросе, но он устарел и не использует MaterialComponents

Я не хочу добавлять панель инструментов на каждый экран, чтобы просто изменить цвет фона.

1 ответ

Вам нужно использовать ThemeOverlay.MaterialComponents.ActionBar или же ThemeOverlay.MaterialComponents.Dark.ActionBar в зависимости от цвета фона.

  <style name="AppTheme" parent="Theme.MaterialComponents.Light">
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryDarkColor</item>
        <item name="colorAccent">@color/secondaryColor</item>
        <item name="actionBarTheme">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@style/ThemeOverlay.MaterialComponents.ActionBar">
        <item name="android:background">#fff</item>
    </style>

Я знаю, что уже слишком поздно.

По материальной составляющей. Вы должны добавить в свою AppTheme:

<style name="AppTheme" parent="Theme.MaterialComponents.Light">
    <!--  This will change ActionBar BackgroundColor to Black -->
    <item name="colorSurface">#000000</item>

    <!--  This will change ActionBar TextColor to White  -->
    <item name="colorOnSurface">#FFFFFF</item>
</style

ПРИМЕЧАНИЕ. Если вы создаете свой проект из шаблона с панелью действий, проверьте свой файл: res/layout/app_bar_main.xml Убедитесь, что вы удалили: android:background="? Attr/colorPrimary" в как показано ниже:

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

   <androidx.appcompat.widget.Toolbar
       android:id="@+id/toolbar"
       android:layout_width="match_parent"
       android:layout_height="?attr/actionBarSize"
  android:background="?attr/colorPrimary" <<== MAKE SURE YOU DELETE THIS ONE LINE
       app:popupTheme="@style/AppTheme.PopupOverlay" />

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

В противном случае ActionBar BackgroundColor будет зависеть от:? Attr/colorPrimary

Поскольку вы используете Theme.MaterialComponents.Light тема приложения цвет фона ActionBar определяется по умолчанию colorPrimary атрибут.

Просто используйте actionBarTheme атрибут для переопределения цвета фона:

  <style name="AppTheme" parent="Theme.MaterialComponents.Light">
    <item name="actionBarTheme">@style/ThemeOverlay.ActionBar</item>
    ...
  </style>

с участием:

  <style name="ThemeOverlay.ActionBar" parent="">
    <item name="colorPrimary">@color/mycolor</item>
  </style>

Вы также можете настроить цвет фона, используя actionBarStyle атрибут в теме вашего приложения.

Что-то вроде:

  <style name="AppTheme" parent="Theme.MaterialComponents.*">
      <item name="actionBarStyle">@style/Custom.ActionBar</item>
      ...
  </style>

В этом случае вы можете использовать background атрибут:

  <style name="Custom.ActionBar" parent="Widget.MaterialComponents.Light.ActionBar.Solid">
    <item name="background">@color/mycolor</item>
  </style>
Другие вопросы по тегам