2016-10-28 22 views
1

我想在我的listviewAdapter上設置自定義字體和字體樣式。如何在ListViewAdapter Android Studio中設置Costum字體?

我試過對這段代碼進行一些修改,但是失敗了。

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

}

回答

1

確保您的字體位於資源文件夾中的項目 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; 
} 
+0

哇,Tankyou蘇海爾·扎希德,這是工作很棒... – JazuliGh

相關問題