我已经阅读了如何在Android中添加字体,但是如何在不是Activity的类中执行此操作。我在src / main / assets / fonts中放了这个字体:lokikola.ttf。
这是我的代码:
MyAdapter.java:
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import at.technikum.mti.fancycoverflow.FancyCoverFlow;
import at.technikum.mti.fancycoverflow.FancyCoverFlowAdapter;
public class MyAdapter extends FancyCoverFlowAdapter{
private int[] images = {R.drawable.rain, R.drawable.forest, R.drawable.wave, R.drawable.leaf};
private String[] nameImage = {"Rain", "Forest", "Wave", "Jungle"};
private Context mContext;
Typeface type;
public MyAdapter(Context mContext){
this.mContext=mContext;
}
@Override
public int getCount() {
return images.length;
}
@Override
public Integer getItem(int i) {
return images[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getCoverFlowItem(int i, View reuseableView, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
reuseableView = inflater.inflate(R.layout.custom_layout, viewGroup, false);
RelativeLayout rlMain =(RelativeLayout) reuseableView.findViewById(R.id.rlMain);
rlMain.setLayoutParams(new FancyCoverFlow.LayoutParams(600,600));
TextView txtName = (TextView) reuseableView.findViewById(R.id.txtNameimage);
ImageView ivImage = (ImageView) reuseableView.findViewById(R.id.imageView);
type = Typeface.createFromAsset(mContext.getAssets(), "fonts/lokikola.ttf");
txtName.setTypeface(type);
txtName.setText(nameImage[i]);
ivImage.setImageResource(images[i]);
return reuseableView;
}
}
当我运行程序时,它在此行显示错误:
type = Typeface.createFromAsset(mContext.getAssets(), "fonts/lokikola.ttf");
那么,如何修复呢?