开发者

Set a typeface of a MenuItem

开发者 https://www.devze.com 2023-02-03 15:20 出处:网络
I have subMenus: @Override public boolean onCreateOptionsMenu(Menu menu) { SubMenu subMenuFile = menu.addSubMenu(\"Font type\");

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.performItemAct开发者_运维知识库ion(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?


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号