我想在listviewAdapter上设置自定义字体和字体样式。
我已尝试对此代码进行一些修改但失败了。
如何在ListviewAdapter
中设置自定义字体?
这是我的代码
package com.istinbat.listview;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter {
Context context;
ArrayList<String> dataNo;
ArrayList<String> dataPetunjuk;
ArrayList<String> dataTeks;
ArrayList<String> dataLatin;
ArrayList<String> dataArti;
public ListAdapter(
Context context2,
ArrayList<String> no,
ArrayList<String> petunjuk,
ArrayList<String> teks,
ArrayList<String> latin,
ArrayList<String> arti
)
{
this.context = context2;
this.dataNo = no;
this.dataPetunjuk = petunjuk;
this.dataTeks = teks;
this.dataLatin = latin ;
this.dataArti = arti ;
}
public int getCount() {
TODO Auto-generated method stub
return dataNo.size();
}
public Object getItem(int position) {
TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
TODO Auto-generated method stub
return 0;
}
public View getView(int position, View child, ViewGroup parent) {
Holder holder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.row, null);
holder = new Holder();
holder.tvNo = (TextView) child.findViewById(R.id.No);
holder.tvPetunjuk = (TextView) child.findViewById(R.id.petunjuk);
holder.tvTeks = (TextView) child.findViewById(R.id.Teks);
holder.tvLatin = (TextView) child.findViewById(R.id.latin);
holder.tvArti = (TextView) child.findViewById(R.id.arti);
child.setTag(holder);
} else {
holder = (Holder) child.getTag();
}
holder.tvNo.setText(dataNo.get(position));
holder.tvPetunjuk.setText(dataPetunjuk.get(position));
holder.tvTeks.setText(dataTeks.get(position));
holder.tvLatin.setText(dataLatin.get(position));
holder.tvArti.setText(dataArti.get(position));
return child;
}
public class Holder {
TextView tvNo;
TextView tvPetunjuk;
TextView tvTeks;
TextView tvLatin;
TextView tvArti;
}
}
答案 0 :(得分:1)
确保您的字体位于项目的assets文件夹中
assets/fonts
public View getView(int position, View child, ViewGroup parent) {
Holder holder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.row, null);
holder = new Holder();
holder.tvNo = (TextView) child.findViewById(R.id.No);
holder.tvPetunjuk = (TextView) child.findViewById(R.id.petunjuk);
holder.tvTeks = (TextView) child.findViewById(R.id.Teks);
holder.tvLatin = (TextView) child.findViewById(R.id.latin);
holder.tvArti = (TextView) child.findViewById(R.id.arti);
Typeface myTypeface = Typeface.createFromAsset(context.getAssets(),"fonts/myFontName.ttf");
//set here the type to you desired textview
holder.tvArti.setTypeface(myTypeface);
child.setTag(holder);
} else {
holder = (Holder) child.getTag();
}
holder.tvNo.setText(dataNo.get(position));
holder.tvPetunjuk.setText(dataPetunjuk.get(position));
holder.tvTeks.setText(dataTeks.get(position));
holder.tvLatin.setText(dataLatin.get(position));
holder.tvArti.setText(dataArti.get(position));
return child;
}