简体   繁体   中英

change color of menu button when pressed in android

I make custom menu item which have color background I want when button press that item button menu selected become to another color like below image:

在此输入图像描述

and its my custom style :

 <style name="Menu">
        <item name="android:colorBackground">@color/white</item>
        <item name="android:textColor">@color/colorAccent</item>
        <item name="android:background">@drawable/item_bg_menu</item>
        </style>

item_bg_menu:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item
    android:state_focused="true"
    android:state_pressed="true"
        android:drawable="@color/white" />

    <item
        android:state_pressed="true"
        android:drawable="@color/gray" />

</selector>

and it is way i use menu:

app:popupTheme="@style/Menu"

The best and easiest way is to make your own style

In this case, style name is Menu

<style name="Menu">
    <item name="android:colorBackground">@color/gray</item>   //color of background menu
    <item name="android:textColor">@color/white</item>       //text color
    <item name="android:listSelector">@drawable/item_bg_menu</item>     //this is for when button press color changed
    </style>

item_bg_menu

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@color/black" />  //color when button press

</selector>

and where you want use that menu in your toll bar just add this app:popupTheme="@style/Menu"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM