Android:如何用font-awesome图标替换导航抽屉图标(可用图标)

时间:2016-04-07 08:28:44

标签: java android fonts font-awesome

我正在开发一个Android项目,其中我有一个导航抽屉,显示GroupSection个对象的列表。所有GroupSection对象都包含一个节图标。抽屉图标由TypedArray和一些特定于Android的语法确定以获取资源。我想用font-awesome图标替换它。每个groupSection对象都有一个getter groupSection.getSectionIcon();,它返回一个icon of icon。我在String.xml中添加了图标,并在assets目录中有fontawesome-webfont.ttf

我无法弄清楚如何用font-awesome图标替换类型化数组。我尝试了findViewByID(R.id.awesome_icon).getId()和其他一些可能性。谢谢。

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

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>

在for-loop中,我可以直接使用getter获取图标String,但设置它是问题所在。你能帮忙的话,我会很高兴。谢谢。 : - )

更新

我尝试了下面的代码,我会手动设置,但我也得到错误:

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

1 个答案:

答案 0 :(得分:1)

字体真棒是文字。因此,如果您想将它们用作图标,则需要将它们转换为可绘制的。

您可以使用此TextDrawable示例: https://stackoverflow.com/a/8831182/3206226