Style appcompat-v7 Toolbar menu background

前端 未结 3 372
鱼传尺愫
鱼传尺愫 2020-12-06 13:25

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, bu

相关标签:
3条回答
  • 2020-12-06 13:50

    You do not use the android namespace when you are using AppCompat attributes. Modify your code as follows:

    <style name="AppToolbarTheme" parent="Theme.AppCompat.NoActionBar">
           <item name="textColorPrimary">@color/white</item>
           <item name="textColorSecondary">@color/cian</item>
    </style>
    
    0 讨论(0)
  • 2020-12-06 14:03

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

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

    And then in your styles.xml add this:-

    <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 讨论(0)
  • 2020-12-06 14:05

    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.
    0 讨论(0)
提交回复
热议问题