[英]How to use Font Awesome Icons in BottomNavigationView in Android
[英]How to set the font awesome icons in menu items in android?
我在字體真棒面臨問題,我嘗試了很多方法來設置菜單項中的字體真棒圖標,但問題沒有解決。
@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;
}
上面的代碼是我的字體很棒的代碼,用於將字體設置為導航抽屜菜單中的菜單項圖標。請幫我解決這個問題。
如何在菜單項中添加字體真棒字體,如上圖所示。
此異常顯示在logcat ..java.lang.NullPointerException中:嘗試在空對象引用上調用接口方法'android.view.MenuItem android.view.Menu.findItem(int)'
您應該從onCreateOptionsMenu(Menu)
或onPrepareOptionsMenu(Menu)
回調中獲得對Menu
的引用。
@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
MenuItem menuItem = menu.findItem(R.id.alerts_id);
... // other actions with menuItem
}
我也遇到了同樣的問題,如果有人仍在努力解決這個問題,可以嘗試以下方式來查看它是否適用於他們
NavigationView navigationView = findViewById(R.id.nav_view); 菜單navMenu = navigationView.getMenu();
FontDrawable drawable = new FontDrawable(this,R.string.fa_sign_out_alt_solid,true,false); drawable.setTextSize(20);
MenuItem logOutItem = navMenu.findItem(R.id.nav_logout);
logOutItem.setIcon(繪制);
屏幕截圖如下所示,以供參考
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.