2012-11-22 27 views
1

設置字體我試圖設置文本視圖的字體用這個方法:的列表視圖

public static final void setAppFont(ViewGroup mContainer, Typeface mFont) 
{ 
    if (mContainer == null || mFont == null) return; 

    final int mCount = mContainer.getChildCount(); 

    // Loop through all of the children. 
    for (int i = 0; i < mCount; ++i) 
    { 
     final View mChild = mContainer.getChildAt(i); 
     if (mChild instanceof TextView) 
     { 
      // Set the font if it is a TextView. 
      ((TextView) mChild).setTypeface(mFont); 
     } 
     else if (mChild instanceof ViewGroup) 
     { 
      // Recursively attempt another ViewGroup. 
      setAppFont((ViewGroup) mChild, mFont); 
     } 
    } 
} 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    // See assets/res/any/layout/styled_text.xml for this 
    // view layout definition. 
    setContentView(R.layout.read_asset); 


    final Typeface mFont = Typeface.createFromAsset(getAssets(), 
      "myfont.otf"); 
      final ViewGroup mContainer = (ViewGroup) findViewById(
      android.R.id.content).getRootView(); 
      MyActivity.setAppFont(mContainer, mFont); 
} 

,但現在我想爲這個列表視圖適配器相同的字體:

public class MainActivity extends Activity { 

// List view 
private ListView lv; 

// Listview Adapter 

ArrayAdapter<String> adapter; 


// Search EditText 
EditText inputSearch; 



// ArrayList for Listview 

ArrayList<HashMap<String, String>> productList; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Listview Data 


String products[] = {"item1", "item2","item3","item4","item5","item6" }; 



    lv = (ListView) findViewById(R.id.list_view); 
    inputSearch = (EditText) findViewById(R.id.inputSearch); 

    // Adding items to listview 

    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products); 
    lv.setAdapter(adapter); 

    /** 
    * Enabling Search Filter 
    * */ 
    inputSearch.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 
      // When user changed the Text 
      MainActivity.this.adapter.getFilter().filter(cs); 
     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
       int arg3) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub 
     } 
    }); 
} 
} 

是否有任何建議? 預先感謝您。

回答

0

這是你所需要的? 我建議您使用自定義列表適配器,它可以幫助您根據需要添加或自定義列表視圖。

如果您需要使用您嘗試改用文字的功能,例如在getView()方法,你可以像這樣

public View getView(int arg0, View arg1, ViewGroup arg2) { 
View convertView = arg1; 

      if(convertView == null) 
      { 
       inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      } 
      convertView = inflater.inflate(R.layout.inflating, arg2, false); 
      TextView mTV_names = (TextView)convertView.findViewById(R.id.inflating_TV_names); 

      // typeface used to display the text in different language other than english 
      // here it is tamil, hindi and telugu. 

      Typeface face = Typeface.createFromAsset(getAssets(), "DroidSansRegionalAAD.ttf"); 

      //setting the typeface to the particular text 

      mTV_names.setTypeface(face); 
      mTV_names.setText(""+countries[arg0]); 
      return convertView; 

     } 
    } 

一次看看check this

所有的東西只是從getView()方法調用setAppFont(convertView, face)

+0

感謝您的回覆。我真的不知道如何處理這個案子。因爲列表視圖中的數據以這種方式添加:String products [] = {「item1」,「item2」,「item3」,「item4」,「item5」,「item6」}; – user1814498

+0

我在底部添加了一個鏈接,可以幫助您。看看那個 –

+0

而CommonsWare建議在函數外部調用createFromAsset以避免它現在被調用。 –

6

只需重寫你的適配器的getView方法:

final Typeface mFont = Typeface.createFromAsset(getAssets(), "myfont.otf"); 
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products) { 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      ViewGroup view = (ViewGroup) super.getView(position, convertView, parent); 
      if(convertView == null) MyActivity.setAppFont(view, mFont); 
      return view; 
     } 
    }; 
+1

請注意,您應該在'getView()'之外調用'createFromAsset()'一次,這樣您就不必在每次調用getView()時瀏覽所有I/O。另外,如果contentView是null,那麼只需調用setAppFont(),因此超類必須創建新的佈局,否則它應該已經有正確的字體。 – CommonsWare

+0

謝謝。我更新了代碼。 – fiddler

+0

謝謝CommonsWare。我試過你的建議,但我仍然有相同的字體。 – user1814498