我的问题很简单:
在我的每个textview中,我目前正在使用该属性
android:fontFamily="sans-serif-light"
Run Code Online (Sandbox Code Playgroud)
在后HC设备上提供华丽的外观.
不幸的是,这不适用于每个小部件,对于我的Spinners,我需要覆盖适配器.
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
那么,有没有办法通过代码获得完全相同的结果?
PS:不,我不想在资产文件夹中添加字体,我只想使用系统一.
sas*_*oar 151
应该可以使用setTypeface()和Typeface.create():
convertView.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
Run Code Online (Sandbox Code Playgroud)
查看文档:
给定一个姓氏和选项样式信息,创建一个字体对象.如果为名称传递null,则将选择"默认"字体.可以查询生成的字体对象(
getStyle())以发现其"真实"样式特征.
请注意,过度使用Typeface.create()会损害您的记忆力,如本评论中所述.该建议的哈希表是一个很好的解决方案,但你必须要修改一点点,因为你没有从资产创建字样.
mur*_*glu 31
Android 4.1(API级别16)和支持库26及更高版本
如果你使用res - > font文件夹,你可以像这样使用
val typeface = ResourcesCompat.getFont(Context, R.font.YOUR_FONT)
TextView.setTypeface(typeface)
Run Code Online (Sandbox Code Playgroud)
Rah*_*rma 18
在我看来,还有一种方法可以在TextView上以编程方式应用系统字体而不会出现任何内存问题,并且使用textview.setTextAppearance方法:
<style name="styleA">
<item name="android:fontFamily">sans-serif</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">?android:attr/textColorPrimary</item>
</style>
<style name="styleB">
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">?android:attr/textColorTertiary</item>
</style>
if(condition){
textView.setTextAppearance(context,R.style.styleA);
}else{
textView.setTextAppearance(context,R.style.styleB);
}
Run Code Online (Sandbox Code Playgroud)
ana*_*ish 12
动态地你可以使用这个在xml中设置类似于android:fontFamily的fontfamily,
For Custom font:
TextView tv = ((TextView) v.findViewById(R.id.select_item_title));
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/mycustomfont.ttf");
tv.setTypeface(face);
For Default font:
tv.setTypeface(Typeface.create("sans-serif-medium",Typeface.NORMAL));
Run Code Online (Sandbox Code Playgroud)
这些是使用的默认字体系列的列表,通过替换双引号字符串"sans-serif-medium"来使用任何一个
FONT FAMILY TTF FILE
1 casual ComingSoon.ttf
2 cursive DancingScript-Regular.ttf
3 monospace DroidSansMono.ttf
4 sans-serif Roboto-Regular.ttf
5 sans-serif-black Roboto-Black.ttf
6 sans-serif-condensed RobotoCondensed-Regular.ttf
7 sans-serif-condensed-light RobotoCondensed-Light.ttf
8 sans-serif-light Roboto-Light.ttf
9 sans-serif-medium Roboto-Medium.ttf
10 sans-serif-smallcaps CarroisGothicSC-Regular.ttf
11 sans-serif-thin Roboto-Thin.ttf
12 serif NotoSerif-Regular.ttf
13 serif-monospace CutiveMono.ttf
Run Code Online (Sandbox Code Playgroud)
"mycustomfont.ttf"是ttf文件.路径将在src/assets/fonts/mycustomfont.ttf中,您可以在此默认字体系列中详细介绍默认字体
选项1 -API 26及更高版本
// Jave
Typeface typeface = getResources().getFont(R.font.myfont);
textView.setTypeface(typeface);
// Kotlin
val typeface = resources.getFont(R.font.myfont)
textView.typeface = typeface
Run Code Online (Sandbox Code Playgroud)
选项2 -API 16及更高版本
// Java
Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);
// Kotlin
val typeface = ResourcesCompat.getFont(context, R.font.myfont)
Run Code Online (Sandbox Code Playgroud)
在Android开发者指南中查看完整的到期日。
通过使用它是可能的
setTypeface(Typeface tf, int style)TextView课堂方法.
spinner_text.setTypeface(Typeface.SANS_SERIF,Typeface.NORMAL);
| 归档时间: |
|
| 查看次数: |
53107 次 |
| 最近记录: |