android - 在android中禁用菜单项的背景高亮颜色

标签 android background selector menuitem

我在我的应用程序菜单中添加了一个项目(带有图标和文本)。

我有两张图片分别表示项目的正常(未按下)状态和突出显示(按下)状态。

这是我的菜单xml

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
         android:showAsAction="ifRoom|withText"
         android:title="New"
         android:icon="@drawable/menu_selector"
         android:id="@+id/add_channel"></item>-
</menu>

这是选择器 menu_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="false"
           android:state_pressed="false"
           android:drawable="@drawable/new_channel" />

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

    <item android:state_selected="true"
          android:state_pressed="false"
          android:drawable="@drawable/book" />
</selector>

但这对我来说还不够。当我按下该项目时,它会更改其包含的图像,但也会在背景中突出显示绿色发光效果。

如何禁用或隐藏这种发光效果?

最佳答案

您可以创建一个带有 ImageButton 和背景选择器的布局,以更改按下状态的颜色。 在您的项目菜单中选择此布局: android:actionLayout="@layout/item_layout"

item_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tab_button"
        android:src="@drawable/ic_menu_item_icon" />

</LinearLayout>

tab_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_enabled="false"
    android:drawable="@drawable/tab_button_background"/>
<item
    android:state_pressed="true"
    android:state_enabled="true"
    android:drawable="@drawable/tab_button_background_pressed" />
<item
    android:state_focused="true"
    android:state_enabled="true"
    android:drawable="@drawable/tab_button_background"
     />
<item
    android:state_enabled="true"
    android:drawable="@drawable/tab_button_background"
     />
</selector>

tab_button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:color="#9ec03b" />
</shape>

tab_button_background_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:color="#dfff53" />
</shape>

菜单.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/add_channel"
        android:showAsAction="ifRoom|withText"
        android:actionLayout="@layout/item_layout"
        android:title="New">
    </item>
</menu>

关于android - 在android中禁用菜单项的背景高亮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15761974/

相关文章:

php - 使用 Select 表单过滤使用 PHP 从 MySQL 查询的表

Android ActionBar 选项卡设置最初选择的选项卡

Android GridView - 根据位置更新 View

html - 背景图片只填充一半的div

Android 线性布局 : not displaying correct height

javascript - 请解释一下这个jquery选择了什么

android - 在 Listview 中模拟用户对 EditText 的触摸操作

android - 支持应用程序内的 Amazon 和 Android 市场(Google Play)链接

Javascript 随机背景右上固定

css 星号选择器支持