Java 更改列表视图中所有列表项的字体
我已经设置了一个可工作的自定义列表视图阵列适配器代码,该代码与显示的代码几乎相似(没有缓存部分) 现在,我如何将所有项目的字体更改为类似roboto的字体 编辑 我试过这个 增加了专用字体文本字体;在oncreate()之前 在xml中:Java 更改列表视图中所有列表项的字体,java,android,listview,fonts,Java,Android,Listview,Fonts,我已经设置了一个可工作的自定义列表视图阵列适配器代码,该代码与显示的代码几乎相似(没有缓存部分) 现在,我如何将所有项目的字体更改为类似roboto的字体 编辑 我试过这个 增加了专用字体文本字体;在oncreate()之前 在xml中: android:typeface 或在java中: setTypeface 使用,您可以更改文本的字体,将desirefontttf文件保存在资产文件夹中,访问并设置desire视图,如下所示: TextView txt = (TextView) find
android:typeface
或在java中:
setTypeface
使用,您可以更改文本的字体,将desirefont
ttf文件保存在资产文件夹中,访问并设置desire视图,如下所示:
TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "roboto.ttf");
txt.setTypeface(font);
有关更多帮助,请检查将字体复制到
assest
文件夹中,并将此代码放入自定义阵列适配器中
TextView yourTextView = (TextView)findViewById(R.id.yourid);
Typeface textFont = Typeface.createFromAsset(context.getAssets(),"YourFont.ttf");
yourTextView.setTypeface(textFont);
它应该会起作用
编辑
专用字体textFont代码>
申报
在OnCreate()
或OnStart()
只需在getView()中使用自定义字体即可
在项目的根目录中创建一个名为assets/fonts/
的文件夹,然后粘贴TTF字体文件(在本例中为roboto.TTF)
然后从适配器的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");
有,
哪里你能发布你的日志吗?我的listadapter设置不一样。。TextView yourTextView=(TextView)listAdapter.getView(0,null,null);Typeface textFont=Typeface.createFromAsset(getApplicationContext().getAssets(),“Roboto BoldCondensed.ttf”);设置字体(textFont);您是否在onCreate
中初始化了字体
。在代码TextView中,您的TextView=(TextView)listAdapter.getView(0,null,null)代码>Typeface textFont=Typeface.createFromAsset(getApplicationContext().getAssets(),“RobotoBoldCondensed.ttf”);设置字体(textFont)代码>删除此行Typeface textFont=Typeface.createFromAsset(getApplicationContext().getAssets(),“RobotoBoldCondensed.ttf”)代码>并将其放入onCreate.抱歉。你什么意思?并张贴您的错误日志。这将有助于找到确切的问题。
yourTextView.setTypeface(textFont);
@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");