如何更改列表项的自定义字体

时间:2016-04-27 07:06:06

标签: android

如何更改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);
    }
});

3 个答案:

答案 0 :(得分:0)

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

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

但我认为你把错误的部分放在这里。可能你想在listview适配器中使用它。

修改

您需要覆盖SimpleAdapter的getView(int position,View convertView,ViewGroup parent)方法并将结果转换为TextView。

从那里开始,设置字体就像往常一样。

Snippet,放在SimpleAdapter的(匿名)扩展名中:

{{1}}

答案 1 :(得分:0)

首先下载所需字体的.ttf文件(helvetica.ttf)。将它放在assets文件夹中(在assest文件夹内创建名为&#34的新文件夹;字体&#34;并将其放入其中)。现在使用TextView初始化下面的以下代码。

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

答案 2 :(得分:0)

通过View的findViewById方法和TextView的setTypeface方法,在自定义适配器中为listview使用自定义适配器,如下所示!

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