简单的ListViewAdapter中的Android Font Face

时间:2013-12-06 18:53:37

标签: android fonts

我正在尝试使用.ttf文件更改ListView中项目的字体。问题是我无法访问getAssets()方法。我该怎么做才能修改我的代码?

public class ListViewArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;

public ListViewArrayAdapter(Context context, String[] values) {
    super(context, R.layout.listview_address, values);
    this.context = context;
    this.values = values;
}

@SuppressLint("ResourceAsColor")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Typeface font = Typeface.createFromAsset(getAssets(), "Raleway-Thin.ttf");
        View rowView = inflater.inflate(R.layout.listview_address, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.selectAddressPt);
        textView.setText(values[position]);
        if(position%2==1){
            textView.setBackgroundResource(R.color.darker_blue);
        }else{
            textView.setBackgroundResource(R.color.light_blue);
        }

        textView.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                SelectAddress.enableButton();
            }

        });

    return rowView;

}

}

2 个答案:

答案 0 :(得分:0)

你试过吗

this.context.getAssets();

答案 1 :(得分:0)

在您的活动中声明字体:

 Typeface font = Typeface.createFromAsset(getAssets(), "Raleway-Thin.ttf");
 //and pass font as argument in constructor.     
 ListViewArrayAdapter(context,values,font);

现在更新您的ListViewArrayAdapter类代码:

public class ListViewArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public Typeface font;

public ListViewArrayAdapter(Context context, String[] values,TypeFace ttf) {
    super(context, R.layout.listview_address, values);
    this.context = context;
    this.values = values;  
    this.font=ttf;
}

@SuppressLint("ResourceAsColor")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.listview_address, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.selectAddressPt);
        textView.setText(values[position]);
        textView.setTypeFace(font);

        if(position%2==1){
            textView.setBackgroundResource(R.color.darker_blue);
        }else{
            textView.setBackgroundResource(R.color.light_blue);
        }

        textView.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                SelectAddress.enableButton();
            }

        });

    return rowView;

}