简体   繁体   English

如何在android中的菜单项中设置字体真棒图标?

[英]How to set the font awesome icons in menu items in android?

在此输入图像描述 I am facing the issue in font awesome ,i tried many ways to set the font awesome icon in menu items but the problem is not solved. 我在字体真棒面临问题,我尝试了很多方法来设置菜单项中的字体真棒图标,但问题没有解决。

   @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.drawer_menu, menu);
    return true;
}


@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    TextDrawable faIcon = new TextDrawable(this);
    faIcon.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
    faIcon.setTextAlign(Layout.Alignment.ALIGN_NORMAL);
    faIcon.setTypeface(FontAwesomeManager.getTypeface(this, FontAwesomeManager.FONTAWESOME));
    faIcon.setText(getResources().getText(R.string.home_font));
    MenuItem menuItem = menu.findItem(R.id.home);
    menuItem.setIcon(faIcon);
    menuItem.setTitle("Home");
    return  true;
}

The above code is my font awesome code for setting the font awesome as menu items icon in navigation drawer menu .Please help me how to solve this. 上面的代码是我的字体很棒的代码,用于将字体设置为导航抽屉菜单中的菜单项图标。请帮我解决这个问题。

How to add the font awesome font in menu items like shown in above image. 如何在菜单项中添加字体真棒字体,如上图所示。

This exception is shown in logcat ..java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.Menu.findItem(int)' on a null object reference 此异常显示在logcat ..java.lang.NullPointerException中:尝试在空对象引用上调用接口方法'android.view.MenuItem android.view.Menu.findItem(int)'

You should get a reference to Menu from either onCreateOptionsMenu(Menu) , or onPrepareOptionsMenu(Menu) callbacks. 您应该从onCreateOptionsMenu(Menu)onPrepareOptionsMenu(Menu)回调中获得对Menu的引用。

@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
    MenuItem menuItem = menu.findItem(R.id.alerts_id);
    ... // other actions with menuItem
}

I also encountered the same issue and if somebody is still struggling with this scenario, can try the following way to see if it works for them 我也遇到了同样的问题,如果有人仍在努力解决这个问题,可以尝试以下方式来查看它是否适用于他们

  1. As the navigation menu items are found in Navigation view, we have to search for NavigationView in onCreateOptionsMenu like below 由于在导航视图中找到了导航菜单项,我们必须在onCreateOptionsMenu中搜索NavigationView,如下所示

NavigationView navigationView = findViewById(R.id.nav_view); NavigationView navigationView = findViewById(R.id.nav_view); Menu navMenu = navigationView.getMenu(); 菜单navMenu = navigationView.getMenu();

  1. I am using a custom Drawable class, "FontDrawable", next step is to initialize an instance of FontDrawable, set the Font Awesome icon, and set its size 我使用的是自定义Drawable类,“FontDrawable”,下一步是初始化FontDrawable的实例,设置Font Awesome图标,并设置其大小

FontDrawable drawable = new FontDrawable(this, R.string.fa_sign_out_alt_solid, true, false); FontDrawable drawable = new FontDrawable(this,R.string.fa_sign_out_alt_solid,true,false); drawable.setTextSize(20); drawable.setTextSize(20);

  1. Find your menu item inside navigation menu 在导航菜单中找到您的菜单项

MenuItem logOutItem = navMenu.findItem(R.id.nav_logout); MenuItem logOutItem = navMenu.findItem(R.id.nav_logout);

  1. Set icon to menu item 将图标设置为菜单项

logOutItem.setIcon(drawable); logOutItem.setIcon(绘制);

Screenshot is shown below for reference 屏幕截图如下所示,以供参考

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM