相当于"android:fontFamily =" sans-serif-light"在Java代码中? [英] What is the equivalent of "android:fontFamily="sans-serif-light" in Java code?
问题描述
我的问题很简单:
在我的每个textview中,我当前使用的是属性
In every of my textview, I am currently using the attribute
android:fontFamily="sans-serif-light"
在HC后设备上提供漂亮的外观.
to provide a gorgeous look on post HC devices.
不幸的是,这不适用于每个小部件,对于Spinners,我需要覆盖适配器.
Unfortunately, this doesn't work with every widget and for my Spinners, I need to overwrite the Adapter.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//You can use the new tf here.
if(convertView == null || convertView.getTag() == null) {
// new view - populate
convertView = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
convertView.setTag(new Object());
}
CheckedTextView spinner_text=(CheckedTextView) convertView.findViewById(android.R.id.text1);
//Typeface should be set here...
return spinner_text;
}
}
那么,有没有办法通过代码获得完全相同的结果?
So, is there a way to get exactly the same result by code?
PS:不,我不想在资产文件夹中放置字体,我只想使用系统一.
PS: No, I don't want to put a typeface in asset folder, I just want to use system one.
推荐答案
使用setTypeface()
和Typeface.create()
应该可以:
convertView.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
请参见文档:
创建具有姓氏和选项样式的字体对象 信息.如果为该名称传递null,则使用默认"字体 将被选择.可以查询生成的字体对象 (
getStyle()
)来发现其真实"样式特征是什么.
Create a typeface object given a family name, and option style information. If null is passed for the name, then the "default" font will be chosen. The resulting typeface object can be queried (
getStyle()
) to discover what its "real" style characteristics are.
请注意,如Typeface.create()对您的内存有害. >此评论. 建议的哈希表是一个很好的解决方案,但您有进行一些修改,因为您不是从资产中创建字体的.
Note that excessively using Typeface.create()
is bad for your memory, as stated in this comment. The suggested Hashtable is a good solution, but you have to modify it a little since you don't create your typeface from an asset.
这篇关于相当于"android:fontFamily =" sans-serif-light"在Java代码中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!