Xamarin Forms Android - Change Toolbar Background

Asked

Viewed 348 times

1

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

Expected result:

inserir a descrição da imagem aqui

Current Result:

inserir a descrição da imagem aqui

Styles.xml

<resources>
   <style name="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’s wrong I can’t identify?

  • You’re doing it in [tag:Xamarin.android], but the question suggests [tag:Xamarin.Forms]. You want the answer to 'What’s wrong I can’t identify?' or to 'how to do in Xamarin.Forms'?

  • I am not doing in Xamarin.Android. It is a project Xamarin Forms Cross Platform.

  • The code you are showing in the question is from the android project.

  • That’s right. It is the Android project that is part of the Cross Solution. To be clear I will change the question

1 answer

0


Specifically responding to the code you posted, the red Arja behind the title is because of that block in style:

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

Simply remove that element (and the reference to it <item name="titleTextAppearance">@style/MyTitleTextAppearance</item>) must solve.

I hope I’ve helped.

  • 1

    This is Diego, it worked. But I found that in my code there was a line that set, in Code Behind the color of the bar. Anyway, I am marking your answer as correct because within the code presented it solved the question. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.