Java 如何在自定义BaseAdapter中使用自定义字体

Java 如何在自定义BaseAdapter中使用自定义字体,java,android,Java,Android,我正在使用Android Studio创建一个Android应用程序。 我在使用自定义SimpleAdapter的活动中有listview。 我需要在自定义适配器中使用自定义字体,但当我尝试时,它不起作用。 没有错误,只是没有使用字体。直接在活动中使用字体路径时效果良好 当我注销创建的fonter时,我得到以下信息: E/====﹕ FONT: android.graphics.Typeface@4c5dfbc0 这是我的自定义适配器代码: package com.myapp.app.util

我正在使用Android Studio创建一个Android应用程序。 我在使用自定义SimpleAdapter的活动中有listview。 我需要在自定义适配器中使用自定义字体,但当我尝试时,它不起作用。 没有错误,只是没有使用字体。直接在活动中使用字体路径时效果良好

当我注销创建的fonter时,我得到以下信息:

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("Your implementations is near to ok. 

But the big problem is you are creating a
TypeFace
instance in
getView()
method which is very resource hungry.

Because
getView()
method calls repeatedly N numbers of time whenever you scroll the list.

And loading resource from assets extensively is bad practice, it may cause
OutOfMemoryError
any time.

So my recommendation is create common object and use in 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;


    }
package com.myapp.app.utilities;
导入android.content.Context;
导入android.graphics.Color;
导入android.graphics.Typeface;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.simpledapter;
导入android.widget.TextView;
导入com.fieldly41.app.R;
导入java.util.ArrayList;
导入java.util.HashMap;
公共类SimpleConAdapter扩展了SimpleAdapter{
私有ArrayList结果;
//私人语境;
字体;
公共SimpleConAdapter(上下文上下文、ArrayList数据、int资源、字符串[]从、int[]到){
超级(上下文、数据、资源、从、到);
这个结果=数据;
}
@凌驾
公共视图getView(内部位置、视图视图、视图组父视图){
视图v=视图;
如果(v==null){
LayoutFlater充气器=(LayoutFlater)parent.getContext().getSystemService(Context.LAYOUT\u充气器\u服务);
v=充气机充气(R.layout.list\u item\u图标,空);
}
if(results.get(position)!=null){
Typeface fonter=Typeface.createFromAsset(v.getResources().getAssets(),“fonts/ss symbolicons line.ttf”);
TextView顶部标签=(TextView)v.findViewById(R.id.top\u标签);
TextView图标\标签=(TextView)v.findViewById(R.id.icon);
TextView-bottom\u-label=(TextView)v.findViewById(R.id.bottom\u-label);

icon_label.setText(“您的实现接近正常

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

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

而且,从资产中大量加载资源是一种不好的做法,它可能会在任何时候导致
OutOfMemoryError

因此,我的建议是创建公共对象并在getView()中使用

并使用“字体”对象代替fonter

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

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

由于它是一个listview,我建议您创建一个自定义的textview,并将其放入行布局xml中

注意:
资产
文件夹中放置所需的字体文件非常重要

使用自定义字体创建自定义文本视图

CustomTextView.java

<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>
尝试定义自定义文本视图,而不是希望显示自定义字体的简单文本视图

列出项目图标.xml


注意自定义TextView声明和初始化,并在使用自定义适配器时尝试使用概念

公共类SimpleConAdapter扩展了SimpleAdapter{
私人语境;
私有ArrayList结果;
公共SimpleConAdapter(上下文上下文、ArrayList数据、int资源、字符串[]从、int[]到){
超级(上下文、数据、资源、从、到);
这个结果=数据;
this.context=context;
}
@凌驾
公共视图getView(内部位置、视图视图、视图组父视图){
视窗座;
如果(视图==null){
holder=新的ViewHolder();
视图=LayoutFlater.from(上下文)。充气(R.layout.list\u item\u图标,空);
holder.top\u label=(TextView)view.findViewById(R.id.top\u label);
holder.icon\u label=(CustomTextView)view.findViewById(R.id.icon);
holder.bottom\u label=(TextView)view.findViewById(R.id.bottom\u label);
视图.设置标签(支架);
}否则{
holder=(ViewHolder)view.getTag();
}

holder.icon\u label.setText(“不从视图获取字体,从上下文获取字体,它的工作方式类似于Typeface.createFromAsset(context.getAssets(),“fontname.ttf”);而不是在适配器中设置自定义字体使用字体创建自定义文本视图。检查:我以前也尝试过,但它没有使用字体。是的,很遗憾我使用了。不,很遗憾没有使用。字体没有被使用。我只看到cymbol,没有看到字体。我用你的代码替换了我的代码,但我仍然无法使用字体。抱歉,再一次n、 不要建议通过丢弃整个代码来解决问题,而只是写下密码!让他们自己研发。@PareshMayani,现在可以了吗?@PareshMayani,你对我的代码有什么问题吗?或者我应该在代码中做些错误的事情吗?现在我意识到你对我的ans很私人。
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);
    }
}
<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>