在Java代码中,“android:fontFamily =“sans-serif-light””的等效代码是什么?

117

我的问题很简单:

在我的每个TextView中,我目前都使用了属性

android:fontFamily="sans-serif-light"

为在后 HC 设备上提供华丽的外观。

不幸的是,这不能适用于每个小部件,对于我的 Spinners,我需要覆盖 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;
    }
}

那么,有没有一种方法可以通过代码获得完全相同的结果呢?

PS:不,我不想把字体放在资源文件夹中,我只想使用系统自带的。

6个回答

181

可以使用setTypeface()Typeface.create()实现:

convertView.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));

请查看文档

创建一个字体对象,给定家族名称和可选样式信息。如果名称为null,则会选择“default”字体。可以查询生成的字体对象(getStyle())以发现其实际的样式特征。

请注意,过度使用Typeface.create()对内存不利,正如此评论中所述。建议使用Hashtable是一种不错的解决方案,但需要进行一些修改,因为您不是从资产创建字体。


3
如果您有许多旋转项目,请注意内存问题--我稍微更新了我的答案。 - saschoar

52

Android 4.1(API级别16)和Support Library 26及以上版本

如果您正在使用res-> font文件夹,则可以像这样使用

  val typeface = ResourcesCompat.getFont(Context, R.font.YOUR_FONT)
  TextView.setTypeface(typeface)

25

您可以通过以下方式动态设置字体族名称,类似于xml中的android: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));

以下是默认字体列表,可以通过替换双引号字符串 "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              

"mycustomfont.ttf" 是 TTF 文件。路径将会在 src/assets/fonts/mycustomfont.ttf,你可以在这个默认字体家族查看更多关于默认字体的信息。


22

在我看来,仍然有一种方法可以在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);
}

2
android:fontFamily 需要 API 16。 - CoolMind
2
使用 TextViewCompat.setTextAppearance(textView, R.style.styleA) - CoolMind

14

选项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

选项2 - API 16及更高版本

// Java
Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);

// Kotlin
val typeface = ResourcesCompat.getFont(context, R.font.myfont)

请在 Android 开发者指南 中查看完整解释。


6

使用TextView类的setTypeface(Typeface tf, int style)方法即可实现。

spinner_text.setTypeface(Typeface.SANS_SERIF,Typeface.NORMAL);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接