listView的自定义视图 - 字体

Mic*_*ert 2 fonts android android-layout android-listview

我有建立自己的字体为我的ListView的问题,我不知道如何使用自己的适配器类和XML是什么,我需要(除了一个在我把ListView控件).我希望(在ListView中)只为具有自己字体的居中文本.那是我的适配器:

public class MyAdapter extends BaseAdapter {

private String[]       objects; // no objects just String array
private final Context   context;

public MyAdapter(Context context, String[] objects) {
    this.context = context;
    this.objects = objects;
}

@Override
public int getCount() {
    return objects.length;
}

@Override
public Object getItem(int position) {
    return objects[position];
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Object obj = objects[position];

    TextView tv = new TextView(context);
    tv.setText(obj.toString());
    Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/kolejRogFont.ttf");
    tv.setTypeface(tf);

    return tv;
}
}
Run Code Online (Sandbox Code Playgroud)

它在Lista.java中调用

ListView lv = new ListView(this);
    lv.setAdapter(new MyAdapter(this, listview_array));
Run Code Online (Sandbox Code Playgroud)

代码来自StackOverFlow上的另一个主题.

  1. 我在线上得到一个错误(未定义的方法):

    字体TF = Typeface.createFromAsset(getAssets(), "字体/ kolejRogFont.ttf");

2.Nothings出现在屏幕上.我应该为ListView布局创建XML吗?它应该包含什么?

ρяσ*_*я K 5

将MyAdapter代码更改为:

public class MyAdapter extends BaseAdapter {

Typeface tf;
private String[]       objects; // no objects just String array
private final Context   context;

public MyAdapter(Context context, String[] objects) {
    this.context = context;
    this.objects = objects;
    tf = Typeface.createFromAsset(context.getAssets(),"fonts/kolejRogFont.ttf");
}
////Your code ...

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Object obj = objects[position];

    TextView tv = new TextView(context);
    tv.setText(obj.toString());
    tv.setTypeface(tf);
    return tv;
}
}
Run Code Online (Sandbox Code Playgroud)