2014-03-30 44 views
0

我正在研究Android應用程序的列表視圖。請告訴我如何使用我的字體作爲List中的TextView如何從ListView中獲取TextView來設置字體? Android

Typeface typeface; 
TextView textView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.menu); 

    String fontPath = "fonts/28.ttf"; 
    typeface = Typeface.createFromAsset(getAssets(), fontPath); 

    ListView menuList = (ListView) findViewById(R.id.ListView_Menu); 

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



    ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, items); 
    menuList.setAdapter(adapt); 

    //View v1=(View)menuList.getItemAtPosition(0); 
+1

。 –

回答

2
  1. 創建包含一個id一個TextView例如row.xml文本

  2. 創建一個自定義適配器並重寫getView()方法,以便在該行中找到textview並設置其字體。

這是代碼,我已經躺在身邊的例子:你需要創建一個客戶適配器,覆蓋getView方法,在這種方法中,設置的TextView的字樣

@Override 
    public View getView (int position, View convertView, ViewGroup parent) 
    { 
     View resultView = convertView; 
     TextView t = null; 

     if (resultView == null) 
     { 
      //row layout defined in xml containing a textview with id: row. 
      LayoutInfalter inflater = LayotuInflater.from(context); 
      resultView = inflater.inflate (R.layout.row, null); 


      t = resultView.findViewById(R.id.text); 
      t.setTypeface(/*typeface here*/) 

      resultView.setTag (holder); 
     }else 
     { 
      t = (TextView) resultView.findViewById(R.id.text); 
     } 
     t.setText(/*text for the item at this position in the list*/) 

     return resultView; 
    } 
相關問題