Xamarin Forms Android - Change Toolbar Background

1

I'm trying to change the background color of the Android Toolbar in a Xamarin Forms application.

Expected result:

CurrentResult:

Styles.xml

<resources><stylename="MyTheme" parent="MyTheme.Base">
  </style>
  <style name="MyTheme.Base" 
         parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">#b71c1c</item>
    <item name="android:textColorPrimary">#ffffff</item>
    <item name="colorPrimaryDark">@color/primaryDark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="android:windowBackground">@color/window_background</item>
    <item name="toolbarStyle">@style/MyToolBarStyle</item>
  </style>
  <style name="MyToolBarStyle" 
         parent="Widget.AppCompat.Toolbar">
    <item name="android:background">#b71c1c</item>
    <item name="android:colorBackground">#b71c1c</item>
    <item name="titleTextAppearance">@style/MyTitleTextAppearance</item>
    <item name="subtitleTextAppearance">@style/MySubTitleTextAppearance</item>
  </style>
  <style name="MyTitleTextAppearance"
         parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:background">#ff0000</item>
  </style>
  <style name="MySubTitleTextAppearance"
         parent="TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
    <item name="android:textColor">#1976d2</item>
  </style>
</resources>

Toolbar.xml

<android.support.v7.widget.Toolbar        
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/MyToolBarStyle"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:layout_scrollFlags="scroll|enterAlways" />

What is wrong that I can not identify?

    
asked by anonymous 08.05.2018 / 19:05

1 answer

0

Responding specifically to the code you posted, the red stripe behind the title is because of this style block:

<style name="MyTitleTextAppearance"
       parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:background">#ff0000</item>
</style>

Simply remove this element (and the reference to it <item name="titleTextAppearance">@style/MyTitleTextAppearance</item> ) should resolve.

I hope I have helped.

    
09.05.2018 / 23:29