gpt4 book ai didi

android - listView 的自定义 View - 字体

转载 作者:太空狗 更新时间:2023-10-29 16:21:42 26 4
gpt4 key购买 nike

我在为我的 ListView 设置自己的字体时遇到问题,我不知道如何使用自己的 Adapter 类以及我需要什么 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;
}
}

它在 Lista.java 中调用

ListView lv = new ListView(this);
lv.setAdapter(new MyAdapter(this, listview_array));

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

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

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

2.屏幕上没有任何显示。我应该为 ListView 布局制作 XML 吗?它应该包含什么?

最佳答案

将 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;
}
}

关于android - listView 的自定义 View - 字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12868982/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com