[英]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 我也遇到了同样的问题,如果有人仍在努力解决这个问题,可以尝试以下方式来查看它是否适用于他们
NavigationView navigationView = findViewById(R.id.nav_view); NavigationView navigationView = findViewById(R.id.nav_view); Menu navMenu = navigationView.getMenu(); 菜单navMenu = navigationView.getMenu();
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);
MenuItem logOutItem = navMenu.findItem(R.id.nav_logout); MenuItem logOutItem = navMenu.findItem(R.id.nav_logout);
logOutItem.setIcon(drawable); logOutItem.setIcon(绘制);
Screenshot is shown below for reference 屏幕截图如下所示,以供参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.