[英]How might I use the “Comic Sans” font for Android's TextView?
[英]How to use `Open Sans` font style for textview in android?
如何在Android中为Textview使用Open Sans
字体样式? 默认情况下,字体家族中的Open Sans
不可用。
使用此lib更改Hole应用程序书法上的字体
使用此代码来更改文字字体。
将字体文件放在资产文件夹中。 就我而言,我创建了一个名为fonts的子目录。
TextView tv = (TextView) findViewById(R.id.textViewName);
Typeface face = Typeface.createFromAsset(getAssets(),"fonts/opansans.ttf");
tv.setTypeface(face);
Android O和Android支持库26添加了对可下载字体的支持。
Google字体在Google Play服务中提供了字体提供程序。
请阅读官方文档: https : //developers.google.com/fonts/docs/android
第1步:创建一个assest文件夹-> fonts文件夹->放置.ttf文件。
第2步:创建您的自定义textview类,如下所示:
public class LotaRegularTextView extends TextView {
public LotaRegularTextView(Context context) {
super(context);
this.setTypeface(Typeface.SERIF);
}
public LotaRegularTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setTypeface(Typeface.SERIF);
}
public LotaRegularTextView(Context context, AttributeSet attrs, int
defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setTypeface(Typeface.SERIF);
}
}
步骤3:将FontsOverride.class添加到您的包中,该类将默认字体家族替换为您的字体家族
public final class FontsOverride {
public static void setDefaultFont(Context context,
String staticTypefaceFieldName, String fontAssetName) {
final Typeface regular = Typeface.createFromAsset(context.getAssets(),
fontAssetName);
replaceFont(staticTypefaceFieldName, regular);
}
protected static void replaceFont(String staticTypefaceFieldName,
final Typeface newTypeface) {
try {
final Field staticField = Typeface.class
.getDeclaredField(staticTypefaceFieldName);
staticField.setAccessible(true);
staticField.set(null, newTypeface);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
步骤4:在create方法的应用程序类中编写代码行。 将“ serif”字体替换为您的字体家族
FontsOverride.setDefaultFont(this, "SERIF", "fonts/Lato-Regular.ttf");
步骤5:如何使用如下所示的自定义textview类
<com.example.widget.LotaRegularTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/padding10"
android:text="Ashish"
android:textColor="@color/gini_gray_color_7d7d7d"
android:textSize="@dimen/s_common_a"/>
您可以检查答案https://stackoverflow.com/a/3651168/6792992您可以按照与建议答案相同的方式使用任何一种字体
您必须添加自定义字体。 首先下载字体档案的.ttf而你的情况,可以发现: https://www.fontsquirrel.com/fonts/open-sans一旦你拥有了它,我建议您在本教程中添加自定义字体: https://开头futurestud .io / tutorials / custom-fonts-on-android-using-font-styles
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.