最近一直忙于做安卓项目,好长时间没写博客了,
就把最近我遇到的问题和把问题解决的思路写出来,
供大家分享吧,有些经验需要当自己真正遇到时,才能真正体会的。
前些天为个人做的一个小软件,需要让界面更加的美化一些,
所以就需要将适配器中的字体给改变,但是网上各种搜,
就是没能解决问题,最后还是在讨论群中,经一位大神的提醒,
我最终还是解决了。具体看下面的代码:
首先是重载ArrayAdapter这个类,并重写其中的getVeiw()方法,详细代码如下:
package lxx.dadangpic;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MyArrayAdater extends ArrayAdapter<String>{
private TextView t;
private Context Maincontext;
public MyArrayAdater(Context context, int textViewResourceId,String[] aStrings) {
super(context, textViewResourceId,aStrings);
Maincontext = context;
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Typeface typeface = Typeface.createFromAsset(Maincontext.getAssets(), "fonts/huawenxingkai.ttf");
for(int i = 0; i < parent.getChildCount(); i++){
View view = parent.getChildAt(i);
if (view instanceof TextView) {
//System.out.println("come here!");
((TextView) view).setTypeface(typeface);
}
}
return super.getView(position, convertView, parent);
}
}
我把它上传到我的资源里面去了,想看的话,可以把它下载下来,研究研究,还是蛮不错的。http://download.csdn.net/detail/aibieqing241/6980101