2014-03-31 51 views
0

需要ListView的一些帮助来设置字体。我设置了字体,现在有一个问题将String设置为ListView AGAING。我应该如何使用循环来做到这一点?Android ListView迭代设置字体/文本

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

我不明白你的问题。 – njzk2

回答

1

getView()在技术上迭代您的ListView的项目,所以应该工作。但更好的方法是子类的TextView,并自动为您设置字体:

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")); 
} 

然后一个更好的办法是使用静态参考字体,所以你不必创建每次你的视图加载,但这比这个简单的例子多一点。