如何在项目点击Android上更改MenuItem背景

时间:2017-05-11 08:08:57

标签: android menuitem

选择菜单项时,更改工具栏MenuItem背景的正确方法是什么。我想改变它以表示已选择MenuItem我尝试了其他答案,但没有帮助。有解决方案吗? Menu

这是我的菜单xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="left"
android:textDirection="ltr"
app:itemBackground="@drawable/drawer_list_selector">

    <item
        android:id="@+id/informacije"
        android:layout_gravity="left|start"
        android:checked="true"
        android:textDirection="ltr"
        android:title="O aplikaciji"
        app:showAsAction="never" />
    <item
        android:id="@+id/podesavanja"
        android:layout_gravity="left|start"
        android:textDirection="ltr"
        android:title="Podešavanja"
        app:showAsAction="never" />

和styles.xml:

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:listDivider">@color/status</item>
    <item name="android:itemBackground">#333333</item>.
    <item name="android:itemTextAppearance">@style/myCustomMenuTextApearance</item>
    <item name="dropDownListViewStyle">@style/PopupMenuListView</item>
    <item name="android:activatedBackgroundIndicator">@drawable/drawer_list_selector</item>
    <item name="android:windowContentOverlay">@null</item>        
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="AppTheme.CustomTheme" >
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowCloseOnTouchOutside">true</item>
</style>

<style name="NavigationViewStyle">
    <item name="android:activatedBackgroundIndicator">@drawable/drawer_list_selector</item>
    <item name="android:textSize">14sp</item> <!-- menu item text size-->
    <item name="android:listPreferredItemHeightSmall">40dp</item><!-- menu item height-->
</style>

<style name="myCustomMenuTextApearance" parent="@android:style/TextAppearance.Widget.IconMenu.Item">
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:textSize">14sp</item>
    <item name="android:listPreferredItemHeightSmall">40dp</item>
    <item name="android:activatedBackgroundIndicator">@drawable/drawer_list_selector</item>
</style>

<style name="PopupMenuListView" parent="@style/Widget.AppCompat.Light.ListView.DropDown">
    <item name="android:divider">#FFFF</item>
    <item name="android:dividerHeight">1dp</item>
    <item name="android:showDividers"></item>
    <item name="android:textDirection">ltr</item>
    <item name="android:activatedBackgroundIndicator">@drawable/drawer_list_selector</item>
</style>

活动:

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.podesavanja) {

        startActivity(new Intent(this, Podesavanja.class));
        return true;
    }


    if (id == R.id.informacije) {

        startActivity(new Intent(this, OAplikacij.class));
        return true;
    }

    return super.onOptionsItemSelected(item);
}

enter code here

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

您应该为该弹出菜单创建列表选择器。以下答案应适用于棒棒糖前后设备

https://stackoverflow.com/a/35749206/5577679