我的问题很简单:
在我的每个 TextView 中,我目前都在使用属性
android:fontFamily="sans-serif-light"
在后 HC 设备上提供华丽的外观。
不幸的是,这不适用于每个小部件,对于我的 Spinner,我需要覆盖适配器。
@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;
}
}
那么,有没有办法通过代码得到完全相同的结果?
PS:不,我不想在 Assets 文件夹中放字体,我只想使用系统一。
最佳答案
setTypeface()
和 Typeface.create()
应该可以实现:
convertView.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
见 Docs :
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()
对您的内存力不利,如 this comment 中所述. suggested Hashtable是一个很好的解决方案,但您必须对其进行一些修改,因为您不是从 Assets 创建字体。
关于android - Java 代码中 "android:fontFamily="sans-serif-light"的等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14343903/