Appcompat-v7 Style Toolbar Menu Background

I am trying to style the appcompat-v7 toolbar to have a different background color for my overflow menu. I tried using themes for my application and styles for my toolbar, but I could not achieve this.

This is my toolbar:

<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:minHeight="?attr/actionBarSize" android:background="?attr/colorPrimary" android:layout_width="match_parent" app:theme="@style/AppToolbarTheme" android:layout_height="wrap_content"> 

Here is the style I created:

  <style name="AppToolbarTheme" parent="Theme.AppCompat.NoActionBar"> <item name="android:textColorPrimary">@color/white</item> <item name="android:textColorSecondary">@color/cian</item> </style> 

My main theme is the Theme.AppCompat.Light extension.

Does anyone know how I can do this? If styles cannot be used, is there any other way to achieve it?

+5
source share
3 answers

Add this to your toolbar item.

 app:popupTheme="@style/ThemeOverlay.YourPopup" 

Then in styles.xml specify popup menu style

 <style name="ThemeOverlay.YourPopup" parent="ThemeOverlay.AppCompat.Light"> <item name="android:colorBackground">@color/mtrl_white_100</item> <item name="android:textColor">@color/mtrl_light_blue_900</item> </style> 

 <style name="ThemeOverlay.YourPopup" parent="ThemeOverlay.AppCompat.Light"> <item name="android:colorBackground">@color/mtrl_white_100</item> <item name="android:textColorPrimary">@color/mtrl_light_blue_900</item> </style> 

Note that you need to use android:colorBackground and never android:background . The latter will be applied to everything that has no background (here the menu itself and each menu item), the first applies only to the pop-up menu.

Update: The same applies to textColorPrimary and textColor .

  • The popup menu item defines android:textColor="?android:textColorPrimary" .
  • android:textColorPrimary is a theme attribute defined for themes.
  • android:textColor is a style attribute defined in widgets.
  • If we defined android:textColor in the theme, this applies to every widget that does not define its own android:textColor .
+32
source

You are not using the android namespace when you are using AppCompat attributes. Change the code as follows:

 <style name="AppToolbarTheme" parent="Theme.AppCompat.NoActionBar"> <item name="textColorPrimary">@color/white</item> <item name="textColorSecondary">@color/cian</item> </style> 
+1
source

Add this to your toolbar in the activity.xml file: -

 app:popupTheme="@style/ThemeOverlay.YourApp" 

And then in your styles.xml file add the following: -

 <style name="ThemeOverlay.YourApp" parent="ThemeOverlay.AppCompat.Light"> <item name="android:colorBackground">@android:color/darker_gray</item> <item name="android:textColorPrimary">@color/TextColorPrimary</item> </style> 
0
source

Source: https://habr.com/ru/post/984276/


All Articles