Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

android - Style appcompat-v7 Toolbar menu background

I'm trying to styling my appcompat-v7 toolbar to have a different background color for my overflow menu. I tried to use the themes for my app and styles for my toolbar, but I was not able to achieve it.

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 extending Theme.AppCompat.Light.

Does anybody knows how can I do that? If is not possible using the styles is there any other way to achieve it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add this to your toolbar element

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

Then in your styles.xml define the 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 would be applied to everything that doesn't have a background (here the menu itself and each menu item), the former applies only to the popup menu.

Update: The same applies to textColorPrimary and textColor.

  • Popup menu item defines android:textColor="?android:textColorPrimary".
  • android:textColorPrimary is a theme attribute, it's defined on themes.
  • android:textColor is a style attribute, it's defined on widgets.
  • If we defined android:textColor in a theme, it would be applied to every widget that doesn't define its own android:textColor.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...