如何在 Android 中更改菜单项的文本颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3519277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the Text color of Menu item in Android?
提问by sunil
Can I change the background color of a Menu item in Android?
我可以更改 Android 中菜单项的背景颜色吗?
Please let me know if anyone have any solution to this. The last option will be obviously to customize it but is there any way for changing the text color without customizing it.
如果有人对此有任何解决方案,请告诉我。最后一个选项显然是自定义它,但是有什么方法可以在不自定义的情况下更改文本颜色。
回答by Muhammad Alfaifi
One simple line in your theme :)
主题中的一个简单行:)
<item name="android:actionMenuTextColor">@color/your_color</item>
回答by Marcus Wolschon
It seems that an
似乎一个
<item name="android:itemTextAppearance">@style/myCustomMenuTextAppearance</item>
in my theme and
在我的主题和
<style name="myCustomMenuTextAppearance" parent="@android:style/TextAppearance.Widget.IconMenu.Item">
<item name="android:textColor">@android:color/primary_text_dark</item>
</style>
in styles.xml change the style of list-items but not menu items.
在styles.xml 中更改列表项的样式,但不更改菜单项的样式。
回答by max.mustermann
You can change the color of the MenuItem
text easily by using SpannableString
instead of String
.
您可以MenuItem
使用SpannableString
代替轻松更改文本的颜色String
。
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.your_menu, menu);
int positionOfMenuItem = 0; // or whatever...
MenuItem item = menu.getItem(positionOfMenuItem);
SpannableString s = new SpannableString("My red MenuItem");
s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
item.setTitle(s);
}
回答by Sudara
If you are using the new Toolbar, with the theme Theme.AppCompat.Light.NoActionBar
, you can style it in the following way.
如果您使用的是带有主题的新工具栏,则Theme.AppCompat.Light.NoActionBar
可以按以下方式对其进行样式设置。
<style name="ToolbarTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textColorPrimary">@color/my_color1</item>
<item name="android:textColorSecondary">@color/my_color2</item>
<item name="android:textColor">@color/my_color3</item>
</style>`
According to the results I got,android:textColorPrimary
is the text color displaying the name of your activity, which is the primary text of the toolbar. android:textColorSecondary
is the text color for subtitle and more options (3 dot) button. (Yes, it changed its color according to this property!) android:textColor
is the color for all other text including the menu.
Finally set the theme to the Toolbar
根据我得到的结果,android:textColorPrimary
是显示您的活动名称的文本颜色,这是工具栏的主要文本。android:textColorSecondary
是字幕和更多选项(3 点)按钮的文本颜色。(是的,它根据这个属性改变了它的颜色!)android:textColor
是包括菜单在内的所有其他文本的颜色。
最后将主题设置为工具栏
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:theme="@style/ToolbarTheme"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"/>
回答by Mihir Trivedi
If you are using menu as <android.support.design.widget.NavigationView />
then just add below line in NavigationView
:
如果您正在使用菜单,<android.support.design.widget.NavigationView />
那么只需在以下行中添加NavigationView
:
app:itemTextColor="your color"
Also available colorTint for icon, it will override color for your icon as well. For that you have to add below line:
也可用于图标的 colorTint,它也会覆盖您图标的颜色。为此,您必须添加以下行:
app:itemIconTint="your color"
Example:
例子:
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextColor="@color/color_white"
app:itemIconTint="@color/color_white"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"/>
Hope it will help you.
希望它会帮助你。
回答by Muralikrishna G S
I went about it programmatically like this:
我像这样以编程方式进行了处理:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.changeip_card_menu, menu);
for(int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
SpannableString spanString = new SpannableString(menu.getItem(i).getTitle().toString());
spanString.setSpan(new ForegroundColorSpan(Color.BLACK), 0, spanString.length(), 0); //fix the color to white
item.setTitle(spanString);
}
return true;
}
回答by Pouya Danesh
as you can see in this questionyou should:
正如你在这个问题中看到的,你应该:
<item name="android:textColorPrimary">yourColor</item>
Above code changes the text color of the menu action items for API >= v21.
上面的代码更改了 API >= v21 的菜单操作项的文本颜色。
<item name="actionMenuTextColor">@android:color/holo_green_light</item>
Above is the code for API < v21
以上是 API < v21 的代码
回答by alijandro
I used the html tag to change a single item's text colour when the menu item is inflated. Hope it would be helpful.
当菜单项膨胀时,我使用 html 标签来更改单个项目的文本颜色。希望它会有所帮助。
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
menu.findItem(R.id.main_settings).setTitle(Html.fromHtml("<font color='#ff3824'>Settings</font>"));
return true;
}
回答by AndrewS
SIMPLEST way to make custom menu color for single toolbar, not for AppTheme
为单个工具栏而不是 AppTheme 制作自定义菜单颜色的最简单方法
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay.MenuBlue">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
</android.support.design.widget.AppBarLayout>
usual toolbar on styles.xml
style.xml 上的常用工具栏
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
our custom toolbar style
我们的自定义工具栏样式
<style name="AppTheme.AppBarOverlay.MenuBlue">
<item name="actionMenuTextColor">@color/blue</item>
</style>
回答by Amir Khorsandi
in Kotlin I wrote these extensions:
在 Kotlin 中,我编写了这些扩展:
fun MenuItem.setTitleColor(color: Int) {
val hexColor = Integer.toHexString(color).toUpperCase().substring(2)
val html = "<font color='#$hexColor'>$title</font>"
this.title = html.parseAsHtml()
}
@Suppress("DEPRECATION")
fun String.parseAsHtml(): Spanned {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(this, Html.FROM_HTML_MODE_LEGACY)
} else {
Html.fromHtml(this)
}
}
and used like this:
并像这样使用:
menu.findItem(R.id.main_settings).setTitleColor(Color.RED)