0

我已经设置了一个有效的自定义列表视图数组适配器代码,与此处显示的代码几乎相似(没有缓存部分)

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

编辑 我试过这个

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

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

4 回答 4

1

在项目的根目录中创建一个名为assets/fonts/然后粘贴 TTF 字体文件(在本例中为 robots.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");
于 2013-01-16T11:28:09.710 回答
0

在 xml 中:

android:typeface

或在java中:

setTypeface
于 2013-01-16T11:22:01.897 回答
0

使用字体,您可以更改文本的字体,将所需的fontttf 文件保存在资产文件夹中,访问并设置为您的所需视图,如下所示:

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

如需更多帮助,请查看快速提示:自定义 Android 字体

于 2013-01-16T11:22:17.770 回答
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"); }

OnCreate()OnStart()

只需在您的getView()

yourTextView.setTypeface(textFont);
于 2013-01-16T11:22:30.517 回答