【问题标题】:Cannot set Font Awesome icons from Strings.xml无法从 Strings.xml 设置 Font Awesome 图标
【发布时间】:2014-11-06 14:53:07
【问题描述】:

我正在尝试在我的 android 应用程序中使用 Font Awesome 图标。我已将我想在 strings.xml 文件中使用的各种图标的 unicode 存储在我的应用程序中,但当我运行我的应用程序时,我得到了这个

我做错了什么?

代码

    public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.drawer_list_item, null);
    }

    TextView imgIcon = (TextView) convertView.findViewById(R.id.icon);
    TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
    TextView txtCount = (TextView) convertView.findViewById(R.id.lock);
    imgIcon.setTypeface(fontFamily);
    imgIcon.setText(navDrawerItems.get(position).getTitle());
    txtTitle.setText(navDrawerItems.get(position).getTitle());

    // displaying count
    // check whether it set visible or not
    if (navDrawerItems.get(position).getCounterVisibility()) {
        txtCount.setText(navDrawerItems.get(position).getCount());
    } else {
        // hide the counter view
        txtCount.setVisibility(View.GONE);
    }

    return convertView;
}

字符串.xml

<string-array name="nav_drawer_items">
    <item>Home</item>
    <item>Register</item>
    <item>Create Pattern</item>
    <item>Forgot Login Details</item>
    <item>Check Service</item>
    <item>Reset Data</item>
    <item>Settings</item>
</string-array>

<array name="nav_drawer_icons">
    <item>\uf015</item>
    <item>\uf0c7</item>
    <item>\uf00a</item>
    <item>\uf1da</item>
    <item>\uf00c</item>
    <item>\uf021</item>
    <item>\uf013</item>
</array>

初始化字体真棒的构造函数

public NavDrawerListAdapter(Context context,
        ArrayList<NavDrawerItem> navDrawerItems) {
    this.context = context;
    this.navDrawerItems = navDrawerItems;
    fontFamily = Typeface.createFromAsset(context.getAssets(),
            "fonts/fontawesome.ttf");
}

【问题讨论】:

  • fontFamily 是在哪里定义的?另请注意,您正在为 imgIcon 分配 txtTitle 的 相同值imgIcon.setText(navDrawerItems.get(position).getTitle()); txtTitle.setText(navDrawerItems.get(position).getTitle());
  • @funkystein 它在我的列表适配器的构造函数中初始化
  • 你能展示一下缺失的部分吗?
  • @funkystein 我更新了我的问题
  • 我可能会在 onCreate() 或 onCreateView() 方法中移动这个fontFamily = Typeface.createFromAsset(context.getAssets(), "fonts/fontawesome.ttf");。并考虑我第一条评论的第二部分。

标签: android unicode font-awesome


【解决方案1】:

我最近使用 android-typeface-textview lib (https://github.com/ragunathjawahar/android-typeface-textview) 将 Fontello (http://fontello.com/) 图标添加到我的应用程序中。下载lib,将你需要的字体添加到你的assets/fonts文件夹,添加

xmlns:geekui="http://schemas.android.com/apk/res-auto"

按照 github 页面的说明添加到您的布局中,并将布局文件中的 TextViews 替换为与以下类似的代码:

<com.mobsandgeeks.ui.TypefaceTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    geekui:customTypeface="fonts/YOUR_FONT.ttf" />

如果您不喜欢使用 3rd 方库,您将能够查看代码(非常简单)以了解如何以正确的方式处理字体。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 2020-09-20
    • 2019-06-12
    • 2014-10-18
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多