2012-09-15 123 views
2

我有一個ListView,我想要做的就是更改字體。我已經將我的字體下載到assets/fonts文件夾。更改textview的字體

如果我的理解對不對,我應該重寫適配器..

這裏是我的代碼:

onCreate.... 


Typeface font1 = Typeface.createFromAsset(getAssets(), "fonts/english.ttf"); 
font1 = Typeface.create(font1, Typeface.BOLD); 

TextView customText = (TextView)findViewById(R.id.ListTextView); 
customText.setTypeface(font1); 



    HashMap<String,String> contentItem = new HashMap<String,String>() 
      { 
       { 
       put(NAME_FIELD,"dudi");     
       } 
      };    
      content.add(contentItem); 



      SimpleAdapter adapter = new SimpleAdapter(this, 
      content, 
      R.layout.show_the_list, 
      new String[]{NAME_FIELD}, 
      new int[]{R.id.ListTextView} ); 




      ListView list1 = (ListView) findViewById(R.id.list); 

      list1.setAdapter(adapter); 

xml文件:

<TextView 
    android:id="@+id/ListTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="40dip" 
    android:textColor="@color/White" > 
</TextView> 

,如果有人可以告訴我代碼,我需要添加或更改,這將是偉大的! 謝謝。

回答

3

其實,當你使用R.id.ListTextView作爲參數SimpleAdapter那麼它通過適配器直接訪問創建視圖,以便它是你在活動onCreate()定義並不是什麼新的每一次,因此,無論使用自定義的TextViewSimpleAdapter在XML中添加字體的字體給它,然後把它作爲參數或寫CustomAdapter,並在它的getView()使用視圖吹氣,並在此方法中設置字體字樣重新定義的TextView ..

+0

謝謝你你的答案。我如何使用字體在xml中編寫它?我嘗試了幾個compination,但沒有成功... – user1673206

+0

其實,因爲我看到你作爲一個初學者,所以它複雜的你創建自定義TExtView和添加字體在XML中。因此,最好編寫一個CustomAdapter,並在其中的getView()中將TypeFace設置爲TextView。 – user370305

+0

o.k .. 有任何我可以使用的例子嗎? 謝謝。 – user1673206