How to change Custom Font of Android Menu Item?

前端 未结 10 2018
夕颜
夕颜 2020-12-05 02:17

I have the following Android Java and XML code. I want to change the font of Menu Items of my app. I know only that we can change the font of TextView using setTypeface but

10条回答
  •  青春惊慌失措
    2020-12-05 03:14

    you have to provide a custom layout for the menu item.

    First, in your menu XML set one of the following

    CASE 1 if you are using the support library:

    
        
    
    

    CASE 2 if you are not using the support library:

    
        
    
    

    Then in the actionLayout (layout_menu_save.xml in my example) write the following:

    
    
    
    
        
    
        
    
    
    

    The background drawable (background_transparent_stateful) is useful for having touch feedback on your custom layout:

    
        
        
        
        
    
    

    This way you will have a text with custom font on the left as a label with an icon on the right.

    Obviously you can customize the layout as you prefer!


    EDIT:
    If you are using the support library and your activity Theme extends the AppCompatTheme you can get the typical Lollipop "ripple effect" for a better looking touch feedback. Just replace the custom background with:

    
        ...
    
    

提交回复
热议问题