有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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

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

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

package com.myapp.app.utilities;

import 安卓.content.Context;
import 安卓.graphics.Color;
import 安卓.graphics.Typeface;
import 安卓.util.Log;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.SimpleAdapter;
import 安卓.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;

    }

}

共 (2) 个答案

  1. # 1 楼答案

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

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

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

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

    自定义文本视图。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);
        }
    }
    

    尝试定义自定义文本视图,而不是希望显示自定义字体的简单文本视图

    列出项目图标。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;
        }
    
    }
    
  2. # 2 楼答案

    您的实现接近正常

    但是最大的问题是,您正在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");
    

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

    icon_label.setTypeface(font);