我可以改变一个菜单项的背景颜色在安卓?
如果有人对此有任何解决方案,请让我知道。最后一个选项显然是自定义,但是否有任何方法可以在不自定义的情况下更改文本颜色。
我可以改变一个菜单项的背景颜色在安卓?
如果有人对此有任何解决方案,请让我知道。最后一个选项显然是自定义,但是否有任何方法可以在不自定义的情况下更改文本颜色。
当前回答
添加textColor如下所示
<style name="MyTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light">
<item name="android:textColor">@color/radio_color_gray</item>
</style>
并在xml文件中的工具栏中使用它
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/MyTheme.PopupOverlay" />
其他回答
我使用的是材质设计,当工具栏在一个小屏幕上时,点击更多选项会显示一个空白的白色下拉框。为了解决这个问题,我想在主AppTheme中添加了这个:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:itemTextAppearance">@style/menuItem</item>
</style>
然后创建了一个样式,你可以将菜单项的textColor设置为你想要的颜色。
<style name="menuItem" parent="Widget.AppCompat.TextView.SpinnerItem">
<item name="android:textColor">@color/black</item>
</style>
父名称widget。appcompat . textview . spinneritem我不认为这太重要,它应该仍然工作。
我找到了,尤利卡!!
在你的应用主题中:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/ActionBarTheme</item>
<!-- backward compatibility -->
<item name="actionBarStyle">@style/ActionBarTheme</item>
</style>
这是你的动作栏主题:
<style name="ActionBarTheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">@color/actionbar_bg_color</item>
<item name="popupTheme">@style/ActionBarPopupTheme</item
<!-- backward compatibility -->
<item name="background">@color/actionbar_bg_color</item>
</style>
这是你弹出的主题:
<style name="ActionBarPopupTheme">
<item name="android:textColor">@color/menu_text_color</item>
<item name="android:background">@color/menu_bg_color</item>
</style>
干杯,)
正如你在这个问题中看到的,你应该:
<item name="android:textColorPrimary">yourColor</item>
上面的代码更改API >= v21菜单操作项的文本颜色。
<item name="actionMenuTextColor">@android:color/holo_green_light</item>
以上是API < v21的代码
简短的回答是肯定的。幸运的你! 要做到这一点,你需要覆盖Android默认样式的一些样式:
首先,看看Android中主题的定义:
<style name="Theme.IconMenu">
<!-- Menu/item attributes -->
<item name="android:itemTextAppearance">@android:style/TextAppearance.Widget.IconMenu.Item</item>
<item name="android:itemBackground">@android:drawable/menu_selector</item>
<item name="android:itemIconDisabledAlpha">?android:attr/disabledAlpha</item>
<item name="android:horizontalDivider">@android:drawable/divider_horizontal_bright</item>
<item name="android:verticalDivider">@android:drawable/divider_vertical_bright</item>
<item name="android:windowAnimationStyle">@android:style/Animation.OptionsPanel</item>
<item name="android:moreIcon">@android:drawable/ic_menu_more</item>
<item name="android:background">@null</item>
</style>
因此,菜单中文本的外观在@android:style/ textappearance . widget。iconmenu . item中 现在,在样式的定义中:
<style name="TextAppearance.Widget.IconMenu.Item" parent="TextAppearance.Small">
<item name="android:textColor">?textColorPrimaryInverse</item>
</style>
现在我们有了问题中的颜色名称,如果你查看系统资源的颜色文件夹:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="@android:color/bright_foreground_light_disabled" />
<item android:state_window_focused="false" android:color="@android:color/bright_foreground_light" />
<item android:state_pressed="true" android:color="@android:color/bright_foreground_light" />
<item android:state_selected="true" android:color="@android:color/bright_foreground_light" />
<item android:color="@android:color/bright_foreground_light" />
<!-- not selected -->
</selector>
最后,以下是你需要做的事情:
TextAppearance.Widget.IconMenu覆盖”。项目”,创造自己的风格。然后将它链接到您自己的选择器,使其成为您想要的方式。 希望这对你有所帮助。 好运!
Options menu in android can be customized to set the background or change the text appearance. The background and text color in the menu couldn’t be changed using themes and styles. The android source code (data\res\layout\icon_menu_item_layout.xml)uses a custom item of class “com.android.internal.view.menu.IconMenuItem”View for the menu layout. We can make changes in the above class to customize the menu. To achieve the same, use LayoutInflater factory class and set the background and text color for the view.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
getLayoutInflater().setFactory(new Factory() {
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
if (name .equalsIgnoreCase(“com.android.internal.view.menu.IconMenuItemView”)) {
try{
LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
// set the background drawable
view .setBackgroundResource(R.drawable.my_ac_menu_background);
// set the text color
((TextView) view).setTextColor(Color.WHITE);
}
});
return view;
} catch (InflateException e) {
} catch (ClassNotFoundException e) {}
}
return null;
}
});
return super.onCreateOptionsMenu(menu);
}