I know that
A style specifies attributes for a particular type of view. For example, one style might specify a button's attributes. Every attribute you specify in a style is an attribute you could set in the layout file. By extracting all the attributes to a style, it's easy to use and maintain them across multiple widgets.
A theme defines a collection of named resources which can be referenced by styles, layouts, widgets, and so on. Themes assign semantic names, like colorPrimary, to Android resources.
So, i suppose that I can set the theme
attribute from a style file.
Why doesn't this work?
<!-- Customize your theme here. -->
<item name="toolbarStyle">MyToolbarStyle</item>
...
<style name="MyToolBarTheme" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<item name="android:textColorPrimary">@color/white</item>
<item name="colorOnPrimary">@color/white</item>
</style>
<style name="MyToolbarStyle" parent="Widget.AppCompat.Toolbar">
<item name="theme">@style/MyToolBarTheme</item>
</style>
Instead this works correctly:
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/homeToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/MyToolBarTheme" />