How to change Custom Font of Android Menu Item?

前端 未结 10 2023
夕颜
夕颜 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 02:54

    The answer by @brahmyadigopula works well.

    Here is just a more complete version for the sake of simplification:

    The Menu

    
        
    
    

    In your Activity or Fragment:

            // Inflater the menu based on the layout above
            inflater.inflate(menuRes, menu);
    
    
            // Set font type face
            if (setCustomFontType){
                final MenuItem menuItem = menu.findItem(R.id.update);
                String title = menuItem.getTitle().toString();
    
                Button button_menu = (Button) menuItem.getActionView();
                button_menu.setTypeface("");
                button_menu.setText(title);
                button_menu.setTextColor(Color.WHITE);
                button_menu.setBackgroundColor(_getResources().getColor(R.color.transparent_color));
                button_menu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onOptionsItemSelected(menuItem);
                    }
                });
            }
    
            super.onCreateOptionsMenu(menu, inflater);
    

提交回复
热议问题