gpt4 book ai didi

android - 如何在 ListView Adapter Android Studio 中设置自定义字体?

转载 作者:行者123 更新时间:2023-11-30 00:55:08 25 4
gpt4 key购买 nike

我想在我的 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;
}

最佳答案

Make sure your font located in assets folder in your project 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;
}

关于android - 如何在 ListView Adapter Android Studio 中设置自定义字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40302234/

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