【问题标题】:How to use custom font in custom BaseAdapter如何在自定义 BaseAdapter 中使用自定义字体
【发布时间】:2014-12-09 19:07:40
【问题描述】:

我正在使用 Android Studio 创建一个 Android 应用。 我在使用自定义 SimpleAdapter 的活动中有列表视图。 我需要在自定义适配器中使用自定义字体,但是当我尝试时它不起作用。 没有错误,只有没有使用字体。直接在活动中使用时,字体路径可以正常工作。

当我注销创建的字体时,我得到了这个:

E/====﹕ FONT: android.graphics.Typeface@4c5dfbc0

这是我的自定义适配器代码:

package com.myapp.app.utilities;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.fieldly41.app.R;

import java.util.ArrayList;
import java.util.HashMap;

public class SimpleIconAdapter extends SimpleAdapter {

    private ArrayList<HashMap<String, String>> results;

    //private Context context;

    Typeface font;

    public SimpleIconAdapter(Context context, ArrayList<HashMap<String, String>> data, int resource, String[] from, int[] to) {

        super(context, data, resource, from, to);

        this.results = data;

    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        View v = view;

        if (v == null) {

            LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            v = inflater.inflate(R.layout.list_item_icon, null);

        }

        if(results.get(position) != null ) {

            Typeface fonter = Typeface.createFromAsset(v.getResources().getAssets(), "fonts/ss-symbolicons-line.ttf");

            TextView top_label = (TextView) v.findViewById(R.id.top_label);
            TextView icon_label = (TextView) v.findViewById(R.id.icon);
            TextView bottom_label = (TextView) v.findViewById(R.id.bottom_label);

            icon_label.setText("????");
            icon_label.setTypeface(fonter);

            if (results.get(position).get("locked").equals("false")) {

                icon_label.setTextColor(Color.WHITE);

            } else {

                icon_label.setTextColor(Color.RED);

            }

            top_label.setText(results.get(position).get("title"));
            bottom_label.setText(results.get(position).get("created_at"));

        }

        return v;

    }

}

【问题讨论】:

  • 不要从视图中获取字体 从上下文中获取它会像 Typeface.createFromAsset(context.getAssets(), "fontname.ttf");
  • 不要在适配器中设置自定义字体,而是使用字体创建自定义 TextView。

标签: java android


【解决方案1】:

您的实现几乎没问题。

但最大的问题是您在 getView() 方法中创建了一个 TypeFace 实例,这非常耗资源。

因为getView() 方法会在您滚动列表时重复调用 N 次。

从资产中广泛加载资源是不好的做法,它可能随时导致OutOfMemoryError

所以我的建议是创建通用对象并在 getView() 中使用。

public SimpleIconAdapter(Context context, ArrayList<HashMap<String, String>> data, int resource, String[] from, int[] to) {

        super(context, data, resource, from, to);
        font=Typeface.createFromAsset(context.getAssets(), "fonts/ss-symbolicons-line.ttf");
        this.results = data;


    }

在 getView() 中删除这一行

Typeface fonter = Typeface.createFromAsset(v.getResources().getAssets(), "fonts/ss-symbolicons-line.ttf");

并使用“font”对象代替fonter

icon_label.setTypeface(font);

【讨论】:

  • 我也试过这个,但是没有使用字体。
  • 是的,很遗憾我做到了。
  • 不,很遗憾没有。未使用该字体。我只看到符号,看不到字体。
【解决方案2】:

试试这个方法,希望能帮助你解决问题。

由于它是一个列表视图,我建议您创建一个自定义文本视图并将其放在行布局 xml 中。

注意:将所需的字体文件放在assets 文件夹中很重要。

使用您的自定义字体创建自定义 TextView。

CustomTextView.java

public class CustomTextView extends TextView {
    public CustomTextView(Context context) {
        super(context);
        setFont();
    }
    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFont();
    }
    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setFont();
    }

    private void setFont() {
        Typeface font = Typeface.createFromAsset(getContext().getAssets(),"fonts/ss-symbolicons-line.ttf");
        setTypeface(font, Typeface.NORMAL);
    }
}

尝试定义自定义 TextView 而不是您想要显示自定义字体的简单 TextView。

list_item_icon.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/top_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <yourpackagename.CustomTextView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"/>

    <TextView
        android:id="@+id/bottom_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"/>
</LinearLayout>

注意自定义TextView的声明和初始化,在使用自定义适配器时尽量使用ViewHolder概念。

public class SimpleIconAdapter extends SimpleAdapter {

    private Context context;
    private ArrayList<HashMap<String, String>> results;

    public SimpleIconAdapter(Context context, ArrayList<HashMap<String, String>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        this.results = data;
        this.context=context;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        ViewHolder holder;

        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.list_item_icon, null);
            holder.top_label = (TextView) view.findViewById(R.id.top_label);
            holder.icon_label = (CustomTextView) view.findViewById(R.id.icon);
            holder.bottom_label = (TextView) view.findViewById(R.id.bottom_label);

            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }

        holder.icon_label.setText("?");

        if (results.get(position).get("locked").equals("false")) {
            holder.icon_label.setTextColor(Color.WHITE);
        } else {
            holder.icon_label.setTextColor(Color.RED);
        }

        holder.top_label.setText(results.get(position).get("title"));
        holder.bottom_label.setText(results.get(position).get("created_at"));

        return view;

    }

    class ViewHolder{
        TextView top_label;
        CustomTextView icon_label;
        TextView bottom_label;
    }

}

【讨论】:

  • 我用你的代码替换了我的代码,但我仍然没有得到要使用/工作的字体。对不起。
  • 再一次,不要通过转储整个代码来建议解决方案,而只是写下要点!让他们自己研发。
  • @PareshMayani,现在可以吗?
  • @PareshMayani,你对我的代码有什么问题吗,或者我应该在我的代码中做错了什么,现在我意识到你对我的回答非常个人化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 2019-02-21
  • 1970-01-01
  • 2019-02-13
  • 2013-04-26
  • 2010-11-23
  • 2017-01-01
相关资源
最近更新 更多