设置 MenuItem 的字体
我有子菜单:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu subMenuFile = menu.addSubMenu("Font type");
SubMenu subMenuEdit = menu.addSubMenu("Font size");
subMenuEdit.add(Menu.NONE, IDM_FONT_SIZE, Menu.NONE, "Change size of font");
return super.onCreateOptionsMenu(menu);
}
我想设置他们的字体。我可以做吗? Menu.XML 文件不支持属性 android:typeface="serif"。编译器显示:“错误:不允许使用字符串类型(在‘typeface’处,值为‘Arial’)” 然后我这样做了:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu subMenuFile = menu.addSubMenu("Font type");
for(int i = 0; i < fontFaces.length; ++i) {
subMenuFile.add(Menu.NONE, IDM_FONT_TYPE, Menu.NONE, Html.fromHtml("<b>html</b>"));
}
SubMenu subMenuEdit = menu.addSubMenu("Font size");
subMenuEdit.add(Menu.NONE, IDM_FONT_SIZE, Menu.NONE, "Change size of font");
return super.onCreateOptionsMenu(menu);
}
我已经看到,子菜单支持不同的样式,但应用程序完成时出现错误。这是日志:
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): FATAL EXCEPTION: main
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): java.lang.IllegalArgumentException: Invalid payload item type
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.util.EventLog.writeEvent(Native Method)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.app.Activity.onMenuItemSelected(Activity.java:2204)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:748)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:137)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:873)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.widget.ListView.performItemClick(ListView.java:3513)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1800)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.os.Handler.handleCallback(Handler.java:587)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.os.Handler.dispatchMessage(Handler.java:92)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.os.Looper.loop(Looper.java:123)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.app.ActivityThread.main(ActivityThread.java:3647)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at java.lang.reflect.Method.invoke(Method.java:507)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at dalvik.system.NativeStart.main(Native Method)
01-12 10:04:22.393: ERROR/InputDispatcher(67): channel '406c4fb8 android.htmleditor/android.htmleditor.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
01-12 10:04:22.393: ERROR/InputDispatcher(67): channel '406c4fb8 android.htmleditor/android.htmleditor.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
01-12 10:04:23.263: ERROR/InputDispatcher(67): Received spurious receive callback for unknown input channel. fd=175, events=0x8
How can I set a typeface of a MenuItem?
I have subMenus:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu subMenuFile = menu.addSubMenu("Font type");
SubMenu subMenuEdit = menu.addSubMenu("Font size");
subMenuEdit.add(Menu.NONE, IDM_FONT_SIZE, Menu.NONE, "Change size of font");
return super.onCreateOptionsMenu(menu);
}
I want to set their typeface. Can I do it? Menu.XML file doesn't support the attribute android:typeface="serif". The compiler says: "Error: String types not allowed (at 'typeface' with value 'Arial')"
Then I have done this:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu subMenuFile = menu.addSubMenu("Font type");
for(int i = 0; i < fontFaces.length; ++i) {
subMenuFile.add(Menu.NONE, IDM_FONT_TYPE, Menu.NONE, Html.fromHtml("<b>html</b>"));
}
SubMenu subMenuEdit = menu.addSubMenu("Font size");
subMenuEdit.add(Menu.NONE, IDM_FONT_SIZE, Menu.NONE, "Change size of font");
return super.onCreateOptionsMenu(menu);
}
I have seen, that the submenu supports different styles, but the application finishes with an error. Here is the log:
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): FATAL EXCEPTION: main
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): java.lang.IllegalArgumentException: Invalid payload item type
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.util.EventLog.writeEvent(Native Method)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.app.Activity.onMenuItemSelected(Activity.java:2204)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:748)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:137)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:873)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.widget.ListView.performItemClick(ListView.java:3513)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1800)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.os.Handler.handleCallback(Handler.java:587)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.os.Handler.dispatchMessage(Handler.java:92)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.os.Looper.loop(Looper.java:123)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at android.app.ActivityThread.main(ActivityThread.java:3647)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at java.lang.reflect.Method.invoke(Method.java:507)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-12 10:04:18.103: ERROR/AndroidRuntime(1078): at dalvik.system.NativeStart.main(Native Method)
01-12 10:04:22.393: ERROR/InputDispatcher(67): channel '406c4fb8 android.htmleditor/android.htmleditor.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
01-12 10:04:22.393: ERROR/InputDispatcher(67): channel '406c4fb8 android.htmleditor/android.htmleditor.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
01-12 10:04:23.263: ERROR/InputDispatcher(67): Received spurious receive callback for unknown input channel. fd=175, events=0x8
How can I set a typeface of a MenuItem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法更改菜单项的样式。您只能更改菜单项的文本和菜单图标。如果您想更改外观和感觉,恐怕您必须创建一个新的自定义实现。
对于初学者,我建议覆盖菜单键并显示您自己的自定义视图而不是默认的 Android 菜单。
You cant change the style of a menu item. You can only change the text and the menu icon of a menu item. If you wan to change the look and feel, i am afraid that you have to create a new custom implementation of yours.
For starters, i will suggest override the menu key and show your own custom view instead of default android menu.
您可以通过 xml 为您的项目提供自定义布局并使用您需要的字体。
请参阅此处的答案:https://stackoverflow.com/a/24807645/4958167
这是一篇文章如何创建带字体的文本视图。
You can provide custom layout for your item and use typeface that you need through xml.
See the answer here: https://stackoverflow.com/a/24807645/4958167
And here is an article how to create typefaced text view.
动态
setTypeface(Typeface.MONOSPACE);
通过setTypeface()
方法我们可以设置视图的字体样式。dynamically
setTypeface(Typeface.MONOSPACE);
throughsetTypeface()
method we can set the font style for the views.