更改列表视图中所有列表项的字体

时间:2013-01-16 11:17:07

标签: java android listview fonts

我已经设置了一个工作自定义列表视图数组适配器代码几乎类似于显示here的一个(没有缓存部分)

现在如何将所有项目的字体更改为roboto

修改 我试过这个

添加了私有字体textFont;在oncreate()之前;

 TextView yourTextView = (TextView) listAdapter.getView(0, null, null);
      TypefacetextFont=Typeface.createFromAsset(getApplicationContext().getAssets(),"RobotoBoldCondensed.ttf");
     yourTextView.setTypeface(textFont);

4 个答案:

答案 0 :(得分:1)

在项目的根目录中创建一个名为assets/fonts/的文件夹,然后粘贴TTF字体文件(在本例中为roboto.ttf)。

然后使用adapter's getview()方法中的方法:

@Override
public View getView ( int position, View convertView, ViewGroup parent ) {

      /* create a new view of my layout and inflate it in the row */
      convertView = ( RelativeLayout ) inflater.inflate( resource, null );

      /* Extract the city's object to show */
      City city = getItem( position );

      /* Take the TextView from layout and set the city's name */
      TextView txtName = (TextView) convertView.findViewById(R.id.cityName);
      txtName.setText(city.getName());

      /* Take the TextView from layout and set the city's wiki link */
      TextView txtWiki = (TextView) convertView.findViewById(R.id.cityLinkWiki);
      txtWiki.setText(city.getUrlWiki());

      Typeface face=Typeface.createFromAsset(getAssets(),"fonts/roboto.ttf");

      txtName.setTypeface(face);
      txtWiki.setTypeface(face);

      return convertView;
}

编辑:

更改此行,

TypefacetextFont=Typeface.createFromAsset(getApplicationContext().getAssets(),"RobotoBoldCondensed.ttf");

用,

textFont=Typeface.createFromAsset(getApplicationContext().getAssets(),"RobotoBoldCondensed.ttf");

答案 1 :(得分:0)

xml中的

android:typeface

或在java中:

setTypeface

答案 2 :(得分:0)

使用Typeface,您可以更改文字的字体,在资源文件夹中保留欲望font ttf文件,访问并设置为您想要的视图,如下所示:

TextView txt = (TextView) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "roboto.ttf");  
txt.setTypeface(font);

如需更多帮助,请查看Quick Tip: Customize Android Fonts

答案 3 :(得分:0)

将字体复制到assest文件夹,并将此代码放入自定义数组适配器

    TextView yourTextView = (TextView)findViewById(R.id.yourid);
    Typeface textFont = Typeface.createFromAsset(context.getAssets(),"YourFont.ttf");
    yourTextView.setTypeface(textFont);

它应该有用。

修改

private Typeface textFont; 声明

@Override
public void onCreate(){

textFont = Typeface.createFromAsset(context.getAssets(),"YourFont.ttf"); }
<{1>}或OnCreate()

中的

并在OnStart()

中使用自定义字体
getView()