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


106

我的问题很简单:

在我的每个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:不,我不想在资产文件夹中放置字体,我只想使用系统一。

Answers:


168

它应该是可能的setTypeface()Typeface.create()

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

参阅文件

创建一个带有姓氏和选项样式信息的字体对象。如果为该名称传递null,则将选择“默认”字体。可以查询生成的字体对象(getStyle()),以发现其“真实”样式特征是什么。

请注意Typeface.create(),如本注释所述,过度使用对您的记忆不利。该建议的哈希表是一个很好的解决方案,但你必须要修改一点点,因为你没有从资产创建字样。


2
如果您有许多微调项,请当心内存问题-我对答案做了一些更新。
2013年

44

Android 4.1(API级别16)和支持库26及更高版本

如果您使用的是res->字体文件夹,则可以这样使用

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

20

我认为仍然有一种方法可以在TextView上以编程方式应用系统字体,而不会出现任何内存问题,并且正在使用textview.setTextAppearancemethod:

<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需要
API16。– CoolMind

2
使用TextViewCompat.setTextAppearance(textView, R.style.styleA)
CoolMind

19

动态地,您可以使用xml在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));

这些是使用的默认字体系列的列表,可以通过替换双引号字符串“ 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中,您可以在此默认字体系列中详细了解默认字体。


13

选项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开发者指南中查看完整的到期日。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.