gpt4 book ai didi

android - 如何更改点击项目的菜单项背景 Android

转载 作者:行者123 更新时间:2023-11-29 01:09:55 27 4
gpt4 key购买 nike

选择菜单项时更改工具栏 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>

在 Activity 中:

      @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

最佳答案

您应该为该弹出菜单制作列表选择器。下面的这个答案应该适用于 Lollipop 之前和之后的设备

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

关于android - 如何更改点击项目的菜单项背景 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43909861/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com