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

我的问题很简单:

在我的每个文本视图中,我当前都在使用属性

android:fontFamily="sans-serif-light"

为后 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;
}
}

那么,有没有办法通过代码得到完全相同的结果呢?

PS: 不,我不想在资产文件夹中放置字体,我只想使用系统一。

69137 次浏览

It should be possible with setTypeface() and Typeface.create():

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

医生:

创建一个给定家族名称和选项样式的字体对象 如果为名称传递 null,则使用“ default”字体 将被选择。生成的字体对象可以被查询 (getStyle())发现它的“真正的”风格特征是什么。

请注意,过度使用 Typeface.create()对您的内存有害,如 此评论所述。Hashtable 建议道是一个很好的解决方案,但是您必须对它进行一些修改,因为您不能从资产创建字体。

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

通过使用

TextView类的 setTypeface(Typeface tf, int style)方法。

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

通过使用以下命令,可以动态地在 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));

These are the list of default font family used, use any of this by replacing the double quotation string “ sans-serif-media”

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/asset/fonts/mycustomfont.ttf中,你可以在这个 默认字体系列中参考更多关于默认字体的信息

Android 4.1(API 级别16)及支援库26或以上

如果使用 res-> font 文件夹,可以这样使用

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

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

Option 2 - API 16 and higher

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


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

查一下 Android 开发者指南的赎罪记录。