How to change Custom Font of Android Menu Item?

前端 未结 10 2020
夕颜
夕颜 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:09

    I found this solution is usefull for me .hope it will help full new surfers.

    your menu main.xml

    
    
      
    
    

    After you inflated the custom menu, you can make a reference to the item of the menu. When you obtain the reference to the menu item, you will be able to customize the item to show icon and Unicode text. Your icon image file has to be stored in the

    res/drawable folder.

    @Override
       public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
    
       //reference to the item of the menu
       MenuItem i=menu.findItem(R.id.item1);
       Button button_menu =(Button) i.getActionView();
    
       if(itemuser!=null){
       // Create Typeface object to use unicode font in assets folder
       Typeface font= Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/arial.ttf");
       // Set unicode font to menu item
       button_menu .setTypeface(font);  
       // Set item text and color
       button_menu .setText(getResources().getString(R.string._text));
       button_menu .setTextColor(Color.WHITE);
       // Make item background transparent
       button_menu .setBackgroundColor(Color.TRANSPARENT);
       // Show icon next to the text
       Drawable icon=getApplicationContext().getResources().getDrawable( R.drawable.user);
       button_menu .setCompoundDrawablesWithIntrinsicBounds( icon, null, null, null );
    }
    
    return true;
    
       }
    

提交回复
热议问题