4

ImageAdapter extends BaseAdapter用来给gridview. Gridview 有两个textviews。我想为其中之一设置自定义字体。使用Typeface font = Typeface.createFromAsset(getAssets(), "BABYCAKE.TTF");inImageAdapter给出错误The method getAssets() is undefined for the type ImageAdapter

ImageAdapter定义为

package com.amit.wozzle;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.LayoutInflater;

public class ImageAdapter extends BaseAdapter
{  
private ArrayList<String> listCountry;  
private ArrayList<String> scorestage;  
private Activity activity;  
Typeface font;

public ImageAdapter(Activity activity,ArrayList<String> listCountry, ArrayList<String> scorestage) {  
    super();  
    this.listCountry = listCountry;  
    this.scorestage = scorestage;  
    this.activity = activity;  
    font = Typeface.createFromAsset(activity.getAssets(), "BABYCAKE.TTF");
}  

@Override  
public int getCount() {  
    // TODO Auto-generated method stub  
    return listCountry.size();  
}  

@Override  
public String getItem(int position) {  
    // TODO Auto-generated method stub  
    return listCountry.get(position);  
}  

@Override  
public long getItemId(int position) {  
    // TODO Auto-generated method stub  
    return 0;  
}  

public static class ViewHolder  
{  
    public ImageView imgViewFlag;  
    public TextView  txtViewTitle; 
    public TextView  txtViewTitle2; 
}  

@Override  
public View getView(int position, View convertView, ViewGroup parent) {  
    // TODO Auto-generated method stub  
    ViewHolder view;  
    LayoutInflater inflator = activity.getLayoutInflater();  

    if(convertView==null)  
    {  
        view = new ViewHolder();  
        convertView = inflator.inflate(R.layout.grid_item, null);  
        view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);  
        view.txtViewTitle2.setTypeface(font);
        view.txtViewTitle2 = (TextView) convertView.findViewById(R.id.textView2);  

        // view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);  
        convertView.setTag(view);  
    }  
    else  
    {  
        view = (ViewHolder) convertView.getTag();  
    }  
    // view.txtViewTitle2.setBackgroundColor(Color.BLUE);
    view.txtViewTitle2.setText(listCountry.get(position)); 
    view.txtViewTitle.setText(scorestage.get(position)); 
    // view.imgViewFlag.setImageResource(scorestage.get(position));  

    return convertView;  
}  
}

请帮忙。

4

3 回答 3

7

尝试

Typeface font = Typeface.createFromAsset(activity.getAssets(), "BABYCAKE.TTF");
于 2012-08-06T09:28:57.067 回答
1

你为什么要在Typeface里面创建对象getViewTypeface占用大量内存,并且会降低您的应用程序的速度,因为内部创建的字体对象数量将减少可用的内存空间getView

相反,字体文件应该只创建一次,并在需要时重复使用。在getView. 如果仅在适配器内部使用它并在适配器构造函数中初始化它,请将其声明为适配器内部的实例变量。而不是每次在 getView 中创建新实例,而是使用该单个实例来设置字体。

对于您的错误,请使用活动实例变量来调用getAssests()

TypeFace font = Typeface.createFromAsset(activity.getAssets(), "fonts/BABYCAKE.TTF");

编辑- 尝试像这样使用它-

class DemoFonts{
    private static TypeFace typeFace;   
    public static TypeFace getTypeFace(Context mContext){
        if(typeface==null){
            typeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/BABYCAKE.TTF");
        }

        return typeface;
    }

}

尝试像上面那样使用。假设您fonts在资产文件夹中有文件夹。

于 2012-08-06T09:33:26.143 回答
1

view.txtViewTitle2 给出空指针异常,因为您试图在初始化之前访问 textView。以下更改应该可以正常工作

view.txtViewTitle2 = (TextView) convertView.findViewById(R.id.textView2)
view.txtViewTitle2.setTypeface(font);
于 2012-08-06T11:37:52.200 回答