2016-04-27 72 views
1

如何更改ListView项目的字体?在这里,我想将字体更改为helvetica常规字体。我的TextView被分配给字符串变量。请帮帮我。先谢谢你。如何更改列表项目的自定义字体

lv.setOnItemClickListener(newandroid.widget.AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick (AdapterView <?> arg0, View view,int arg2, long arg3){ 

     String countryname = ((TextView) view.findViewById(R.id.countryname)).getText().toString();//this is the textview id assigned to the string variable// 
     String coutrylist = "Algeria:DZ:213:49,Adrar;27,Ain Defla;43,Ain Temouchent"// these are the some of the list items in the list view// 
     String split[] = coutrylist.split("\\$"); 
     // creating new HashMap 
     String cid = ""; 
     String ccode = ""; 

     for (int i = 0; i < split.length; i++) { 
      if (split[i].startsWith(countryname)) { 
       String split3[] = split[i].split("\\:"); 
       cid = split3[2]; 
       ccode = split3[1]; 
      } 
     } 
     // On selecting single track get song information 
     Intent i = new Intent(getApplicationContext(), Npanxx.class); 
     //Toast.makeText(getApplicationContext(), "seleceted : " + countryname, Toast.LENGTH_SHORT).show(); 
     i.putExtra("country", countryname); 
     i.putExtra("code", ccode); 
     i.putExtra("id", cid); 
     startActivity(i); 
    } 
}); 

回答

0

猜测你正在使用工作室。将字体粘贴到src/main/assets/fonts/helveticaregular.ttf中。并使用此代码来设置字体。

Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/helveticaregular.ttf"); 
      txtView.setTypeface(myTypeface); 

但我想你把这里的代码错了。可能你想在你的列表视图适配器中使用它。

编辑

你需要重写SimpleAdapter的getView(INT位置,查看convertView,父母的ViewGroup)方法,并把结果给一个TextView。

从那里,设置字体照常工作。

片段,放置SimpleAdapter的(匿名)扩展中:

Typeface mTypeface = Typeface.createFromAsset(getAssets(), "fonts/helveticaregular.ttf"); // only needs to be initialised once. 

@Override public View getView(int position, View convertView, ViewGroup parent) { 
    View view = super.getView(position, convertView, parent); 
    TextView countryname = ((TextView)view.findViewById(R.id.countryname)); 
    countryname.setTypeface(mTypeface); 
    return view; 
} 
+0

我指定我的TextView的id变量如何typface设置为国名 –

+0

保护无效onPostExecute(字符串FILE_URL){ \t \t \t //获取所有专辑 \t \t \t pDialog.dismiss后关闭该对话框();从后台线程 \t \t \t runOnUiThread(新的Runnable(){ \t \t \t \t公共无效的run(){ \t \t \t \t \t/** \t \t \t \t \t *更新 \t \t \t //更新UI解析JSON数据到ListView中 \t \t \t \t \t * */ \t \t \t \t \t ListAdapter适配器=新SimpleAdapter( \t \t \t \t \t \t \t CoutryList.this,albumsList, \t \t \t \t \t \t \t R.layout。country_list_item2,新的String [] {\t TAG_NAME,TAG_ID},新INT [] { \t \t \t \t \t \t \t \t \t R.id.countryname,R.id.countryflag}); \t \t \t \t \t \t \t \t \t \t //更新列表视图 \t \t \t \t \t setListAdapter(适配器); \t \t \t \t} \t \t \t}); \t \t} –

0

首先下载的字体.TTF文件,你需要(helvetica.ttf)。将它放在资产文件夹(Inside assest文件夹中创建一个名为“fonts”的新文件夹,并将其放在里面)。现在在TextView初始化下面使用以下一段代码。

Typeface myFont = Typeface.createFromAsset(getAssets(),"fonts/helvetica.ttf"); 
countryname.setTypeface(myFont); 
0

使用自定义适配器,通过查看的findViewById方法和TextView中像下面setTypeface方法列表视图中的自定义适配器!

public View getView(int position, View convertView, ViewGroup parent) 

{ 

View view = super.getView(position, convertView, parent); 

TextView countryname = ((TextView)view.findViewById(R.id.countryname));        

//put some custom font in your assets directory 
countryname.setTypeface(Typeface.createFromAsset 
    (view.getContext().getAssets(),"yourfonttype.otf"));// you can also use 
                 your context instead 
                 of view.getContext() 
                 that was asign from 
                 activity 

return view; 
} 
相关问题