设置 MenuItem 的字体

发布于 2024-10-11 10:48:16 字数 4065 浏览 2 评论 0原文

我有子菜单:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

圈圈圆圆圈圈 2024-10-18 10:48:16

您无法更改菜单项的样式。您只能更改菜单​​项的文本和菜单图标。如果您想更改外观和感觉,恐怕您必须创建一个新的自定义实现。

对于初学者,我建议覆盖菜单键并显示您自己的自定义视图而不是默认的 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.

戈亓 2024-10-18 10:48:16

您可以通过 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.

萌无敌 2024-10-18 10:48:16

动态setTypeface(Typeface.MONOSPACE);通过setTypeface()方法我们可以设置视图的字体样式。

dynamically setTypeface(Typeface.MONOSPACE);through setTypeface()method we can set the font style for the views.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文