2014-03-31 59 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")); 
} 

然後一個更好的辦法是使用靜態參考字體,所以你不必創建每次你的視圖加載,但這比這個簡單的例子多一點。