繁体   English   中英

如何在4.0.3中更改fontfamily?

[英]How change fontfamily in 4.0.3?

从android 4.1 / 4.2开始,可以使用以下Roboto字体系列:

 android:fontFamily="sans-serif"           // roboto regular
 android:fontFamily="sans-serif-light"     // roboto light
 android:fontFamily="sans-serif-condensed" // roboto condensed
 android:fontFamily="sans-serif-thin"      // roboto thin (android 4.2)

但是对于版本4.0.3,我需要类似的东西。 有人知道该怎么做吗?

如您之前所说,V4.2 sans-serif-thin不适用于低于此版本的设备。

但是,您可以通过将.ttf.otf文件放入/assets文件夹并使用以下代码,来将字体文件与APK一起放置:

TextView txt = (TextView) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");  
txt.setTypeface(font);

这是完整的示例: http : //mobile.tutsplus.com/tutorials/android/customize-android-fonts/

您也可以在此处获取sans-serif-thin字体: http : //www.fontsquirrel.com/fonts/roboto

这个SO问题

Android不允许您从XML布局中设置自定义字体。 相反,您必须将特定字体文件捆绑在应用程序的资产文件夹中,并以编程方式进行设置。 就像是:

TextView textView = (TextView) findViewById(<your TextView ID>);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "<file name>");
tv.setTypeface(typeFace);

请注意,只有在调用setContentView()之后才能运行此代码。 另外,Android仅支持某些字体,并且应采用.ttf(TrueType)或.otf(OpenType)格式。 即使那样,某些字体也可能不起作用。

编辑

在此处输入图片说明

在这里您可以下载.ttf文件。

在资源文件夹中安装/保留字体,并以编程方式将其分配到文本视图。

Typeface mDesiredFace= Typeface.createFromAsset(getAssets(),"sans-serif.ttf");
textView.setTypeface(mDesiredFace);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM