简体   繁体   English

Android:如何用字体很棒的图标(可用图标)替换导航抽屉图标

[英]Android : How to replace navigation-drawer icons with font-awesome icons(icons available)

I am working on an Android project in which I have a navigation drawer which shows a list of GroupSection objects.我正在开发一个 Android 项目,其中有一个导航抽屉,其中显示了GroupSection对象的列表。 All the GroupSection objects contain a section icon.所有 GroupSection 对象都包含一个部分图标。 The drawer icons are determined by a TypedArray and some android specific syntax to get the resource.抽屉图标由 TypedArray 和一些 android 特定语法确定以获取资源。 I want to replace that with a font-awesome icon.我想用字体很棒的图标替换它。 Each groupSection object has a getter groupSection.getSectionIcon();每个 groupSection 对象都有一个 getter groupSection.getSectionIcon(); which returns me the String of icon.它返回我的图标字符串。 I have added the icons in String.xml and have the fontawesome-webfont.ttf in assets directory.我在 String.xml 中添加了图标,并在 assets 目录中有fontawesome-webfont.ttf

I just cannot figure out how to replace the typed array with font-awesome icon.我只是不知道如何用字体真棒图标替换类型化数组。 I tried findViewByID(R.id.awesome_icon).getId() , and some other possibilites.我尝试了findViewByID(R.id.awesome_icon).getId()和其他一些可能性。 Thank you.谢谢你。

COde :代码 :

  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()));
        }

Sample of string.xml : string.xml 示例:

 <string name="pinterest">&#xf0d2;</string>
    <string name="pinterest_square">&#xf0d3;</string>
    <string name="google_plus_square">&#xf0d4;</string>
    <string name="google_plus">&#xf0d5;</string>
    <string name="money">&#xf0d6;</string>
    <string name="caret_down">&#xf0d7;</string>
    <string name="caret_up">&#xf0d8;</string>
    <string name="caret_left">&#xf0d9;</string>
    <string name="caret_right">&#xf0da;</string>
    <string name="columns">&#xf0db;</string>

As in the for-loop I can directly get the icon String with getter, but setting it is where the issue lies.在 for 循环中,我可以使用 getter 直接获取图标字符串,但设置它是问题所在。 Any help would be nice.你能帮忙的话,我会很高兴。 Thank you.谢谢你。 :-) :-)

Update更新

I tried the below code where I would manually set, but I get error for that as well :我尝试了以下手动设置的代码,但我也遇到了错误:

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)

Code :代码 :

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()));
            }
        }

Font awesome is text.字体真棒是文字。 So if you want use them as icons u need to convert them to drawable.因此,如果您想将它们用作图标,则需要将它们转换为可绘制的。

You can use this TextDrawable example: https://stackoverflow.com/a/8831182/3206226您可以使用此TextDrawable示例: https : TextDrawable

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

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