【问题标题】:Disable background highlight color of menu item in android在android中禁用菜单项的背景突出显示颜色
【发布时间】:2013-03-23 14:54:35
【问题描述】:

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

我有两个图像用于项目的正常(非按下)状态和突出显示(按下)状态。

这是我的菜单 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>

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

如何禁用或使这种发光效果不可见?

【问题讨论】:

标签: android background selector menuitem


【解决方案1】:

您可以使用 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>

menu.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>

【讨论】:

    【解决方案2】:

    如果你在项目上有一个点击监听器,你可以把这段代码放在处理程序中:

    menuItem.setChecked(false);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-17
      • 2014-12-22
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 2013-09-29
      • 2011-03-24
      • 2014-03-14
      相关资源
      最近更新 更多