简体   繁体   English

如何在Android的TextView中使用`Open Sans`字体样式?

[英]How to use `Open Sans` font style for textview in android?

How to use Open Sans font style for textview in android? 如何在Android中为Textview使用Open Sans字体样式? by default in font-family Open Sans is not available. 默认情况下,字体家族中的Open Sans不可用。

use this lib for font change on hole app Calligraphy 使用此lib更改Hole应用程序书法上的字体

use this code for change perticuler text font. 使用此代码来更改文字字体。

put the font file in your assets folder. 将字体文件放在资产文件夹中。 In my case I created a subdirectory called fonts. 就我而言,我创建了一个名为fonts的子目录。

    TextView tv = (TextView) findViewById(R.id.textViewName);
    Typeface face = Typeface.createFromAsset(getAssets(),"fonts/opansans.ttf");
    tv.setTypeface(face);

Android O and Android Support Library 26 add support for Downloadable Fonts. Android O和Android支持库26添加了对可下载字体的支持。

Google Fonts is shipping a Font Provider in Google Play Services. Google字体在Google Play服务中提供了字体提供程序。

Please read official doc: https://developers.google.com/fonts/docs/android 请阅读官方文档: https : //developers.google.com/fonts/docs/android

在此处输入图片说明

Step 1 : create an assest folder -> fonts folder -> place your .ttf file. 第1步:创建一个assest文件夹-> fonts文件夹->放置.ttf文件。

Step 2 : Create your custom textview class like below : 第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);
    }
}

Step 3 : Add FontsOverride.class to your package this class replace default font family to your font family 步骤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();
        }
    }
  }

Step 4 : Write line in application class on create method. 步骤4:在create方法的应用程序类中编写代码行。 that replace "serif" font to your font family 将“ serif”字体替换为您的字体家族

FontsOverride.setDefaultFont(this, "SERIF", "fonts/Lato-Regular.ttf");

Step 5 : How can use custom textview class like below 步骤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您可以按照与建议答案相同的方式使用任何一种字体

You must add a custom font. 您必须添加自定义字体。 First download the font archive .ttf which in your case can be found at : https://www.fontsquirrel.com/fonts/open-sans once you have it I recommend you this tutorial to add custom fonts: https://futurestud.io/tutorials/custom-fonts-on-android-using-font-styles 首先下载字体档案的.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.

相关问题 我如何为 Android 的 TextView 使用“Comic Sans”字体? - How might I use the “Comic Sans” font for Android's TextView? 如何在 android 上没有任何 TextView 的字符串上使用自定义字体样式 - how to use custom font style on string without any TextView on android 如何在我的 Android 应用程序中使用新的 Google Sans 字体? - How to use new Google's Sans font in my Android App? 如何在android中将样式或字体设置为TextView的文本? - How to set style or font to text of a TextView in android? 如何将字体样式添加到 textview android kotlin - How to add font style to textview android kotlin 如何在Android中将Bungee字体样式赋予textview? - How to give Bungee font style to textview in android? 如何在 Android 中使用 fontFamily open sans semi bold 在 TextView 中将单词设置为粗体? - How can I set a word as bold in a TextView with fontFamily open sans semi bold in Android? 如何在android textview中进行文本对齐,同时使用自定义字体样式? - How to text-justify in android textview and at the same time use custom font style? 如何在 Android TextView 中将字体样式设置为粗体、斜体和下划线? - How to set the font style to bold, italic and underlined in an Android TextView? Android应用程序中的Google Product Sans字体,可以使用吗? - Google Product Sans font in Android Application, okay to use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM