ActionBar MenuItem selector [closed]

浪子不回头ぞ 提交于 2019-12-07 13:32:45

问题


Is it possible to change the Image of a MenuItem when pressed and can that be done by a selector (different MenuItems should be changed with a different image when pressed or selected).

Some sample code would be nice.

I looked up a lot of solutions but not many of them made a clear explanation. Cheers !


回答1:


Is it possible to change the Image of a MenuItem when pressed and can that be done by a selector

Yep, absolutely. Here's an example:

Add a selector to your drawable folder.

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

    <!-- Pressed state -->
    <item android:drawable="@drawable/ic_action_your_pressed_icon" android:state_pressed="true"/>
    <!-- Default state -->
    <item android:drawable="@drawable/ic_action_your_default_icon"/>

</selector>

Apply the selector to your MenuItem using the icon attribute:

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

    <item android:icon="@drawable/your_menu_item_selector" ... />

</menu>


来源:https://stackoverflow.com/questions/23795871/actionbar-menuitem-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!