需要ListView帮助设置字体。我设置字体,现在有一个问题,将String设置为ListView AGAING。我应该如何使用循环来做到这一点?
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, items){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String[] items = {
getResources().getString(R.string.menu_item_play),
getResources().getString(R.string.menu_item_settings),
getResources().getString(R.string.menu_item_help),
getResources().getString(R.string.menu_item_exit)
};
String fontPath = "fonts/28.ttf";
typeface = Typeface.createFromAsset(getAssets(), fontPath);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.menu_item, null, false);
TextView textView = (TextView) view.findViewById(R.id.text);
textView.setText(items[0]); // right here must be a loop or smt
textView = (TextView) view.findViewById(R.id.text);
textView.setText(items[1]);
textView.setTypeface(typeface);
return view;
}
};
答案 0 :(得分:1)
getView()在技术上是迭代你的ListView的项目,所以这应该工作。但更好的方法是将TextView子类化并让它自动为您设置TypeFace:
public class CustomTextView extends TextView{
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomTextView(Context context) {
super(context);
init();
}
public void init(boolean bold) {
setTypeface(Typeface.createFromAsset(getAssets(), "fonts/28.ttf"));
}
然后一个更好的方法是使用字体的静态引用,这样你就不必每次加载View时都创建它,但这比这个简单的例子更多。