ActionBar MenuItem选择器 [英] ActionBar MenuItem selector
问题描述
是否可以在按下菜单时更改MenuItem的图像,并且可以通过选择器来完成(按下或选择时,不同的MenuItem应当以不同的图像进行更改).
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).
一些示例代码会很好.
我查找了很多解决方案,但是其中没有一个做出清晰的解释.干杯!
I looked up a lot of solutions but not many of them made a clear explanation. Cheers !
推荐答案
按下时是否可以更改MenuItem的图像,并且可以 由选择器完成
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:
将选择器添加到您的drawable
文件夹.
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>
使用icon
属性将选择器应用于MenuItem
:
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>
这篇关于ActionBar MenuItem选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!