ActionBar MenuItem选择器 [英] ActionBar MenuItem selector

查看:55
本文介绍了ActionBar MenuItem选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在按下菜单时更改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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆