[英]Android : How to replace navigation-drawer icons with font-awesome icons(icons available)
我正在開發一個 Android 項目,其中有一個導航抽屜,其中顯示了GroupSection
對象的列表。 所有 GroupSection 對象都包含一個部分圖標。 抽屜圖標由 TypedArray 和一些 android 特定語法確定以獲取資源。 我想用字體很棒的圖標替換它。 每個 groupSection 對象都有一個 getter groupSection.getSectionIcon();
它返回我的圖標字符串。 我在 String.xml 中添加了圖標,並在 assets 目錄中有fontawesome-webfont.ttf
。
我只是不知道如何用字體真棒圖標替換類型化數組。 我嘗試了findViewByID(R.id.awesome_icon).getId()
和其他一些可能性。 謝謝你。
代碼 :
public void set(List<RestSection> restSectionList, TypedArray navMenuIcons, Long groupId,
int canvasid, int sectionId) {
// Below are default icons
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
for (RestSection restSection : restSectionList) {
// As you can see I am accessing the icons with there resource id.
navDrawerItems.add(new DrawerModel(restSection.getMsectionname(),
navMenuIcons.getResourceId(0, -1), restSection.getMsectionid()));
}
string.xml 示例:
<string name="pinterest"></string>
<string name="pinterest_square"></string>
<string name="google_plus_square"></string>
<string name="google_plus"></string>
<string name="money"></string>
<string name="caret_down"></string>
<string name="caret_up"></string>
<string name="caret_left"></string>
<string name="caret_right"></string>
<string name="columns"></string>
在 for 循環中,我可以使用 getter 直接獲取圖標字符串,但設置它是問題所在。 你能幫忙的話,我會很高興。 謝謝你。 :-)
更新
我嘗試了以下手動設置的代碼,但我也遇到了錯誤:
Unable to find resource: 2131165836
android.content.res.Resources$NotFoundException: File from drawable resource ID #0x7f07028c
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2640)
at android.content.res.Resources.loadDrawable(Resources.java:2540)
at android.content.res.Resources.getDrawable(Resources.java:806)
at android.content.Context.getDrawable(Context.java:458)
at android.widget.ImageView.resolveUri(ImageView.java:811)
at android.widget.ImageView.setImageResource(ImageView.java:418)
代碼 :
for (RestSection restSection : restSectionList) {
if (restSection.getSectionIcon() != null) {
DrawerModel drawerModel = new DrawerModel();
drawerModel.setTitle(restSection.getMsectionname());
drawerModel.setId(restSection.getMsectionid());
drawerModel.setIcon(R.string.try_icon);
navDrawerItems.add(drawerModel);
} else {
navDrawerItems.add(new DrawerModel(restSection.getMsectionname(),
navMenuIcons.getResourceId(0, -1), restSection.getMsectionid()));
}
}
字體真棒是文字。 因此,如果您想將它們用作圖標,則需要將它們轉換為可繪制的。
您可以使用此TextDrawable
示例: https : TextDrawable
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.