【问题标题】:How to add external fonts to android application如何将外部字体添加到 android 应用程序
【发布时间】:2016-06-17 18:20:39
【问题描述】:

我正在为我的 android 应用程序寻找一些时尚的字体。但问题是如何让我的 android 应用程序支持外部字体。

谢谢。

【问题讨论】:

    标签: android android-fonts


    【解决方案1】:

    您需要在项目的 assets 文件夹下创建 fonts 文件夹,并将您的 TTF 放入其中。然后在你的 Activity onCreate()

    TextView myTextView=(TextView)findViewById(R.id.textBox);
    Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf");
    myTextView.setTypeface(typeFace);
    

    请注意,并非所有 TTF 都能正常工作。在我进行实验时,它只适用于一个子集(在 Windows 上,那些名称是用小写大写字母写的)。

    【讨论】:

    • 这个资产文件夹在哪里?我正在使用 android studio,我可以在 src/ 下找到它
    • 不在src下,assets文件夹应该在你的主项目文件夹下(同级:src、res、gen、bin...)检查:developer.android.com/tools/projects/index.html
    • @Zelimir 是不是有一种方法可以为该项目提供普遍的反对意见?而不是每个 TextView 设置它的字体?
    • @libathos - 听起来很合理,但我不知道该怎么做。没时间调查,是吗?
    • @libathos 好吧,你可以通过 xml 定义字体,但这实际上比在编码中需要更多的写作
    【解决方案2】:

    您可以使用自定义字体为整个应用程序使用自定义 TextView 这是一个示例

    public class MyTextView extends TextView {
    
       Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), Constants.FONT_REGULAR);
       Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(),  Constants.FONT_BOLD);
    
       public MyTextView(Context context, AttributeSet attrs, int defStyle) {
           super(context, attrs, defStyle);
       }
    
       public MyTextView(Context context, AttributeSet attrs) {
           super(context, attrs);
       }
    
       public MyTextView(Context context) {
           super(context);
       }
    
       public void setTypeface(Typeface tf, int style) {
           if (style == Typeface.BOLD) {
               super.setTypeface(boldTypeface/*, -1*/);
           } else {
               super.setTypeface(normalTypeface/*, -1*/);
           }
       }
    }
    

    【讨论】:

      【解决方案3】:

      在 assets 文件夹中创建一个名为 fonts 的文件夹,并从下面的链接添加 sn-p。

      Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/fontname.ttf");
      textview.setTypeface(tf);
      

      【讨论】:

        【解决方案4】:

        要实现你需要使用字体通过下面的示例

        Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
        for (View view : allViews)
        {
           if (view instanceof TextView) 
           {
              TextView textView = (TextView) view;
              textView.setTypeface(typeface);
              }
           }
        }
        

        【讨论】:

        • 对于allViews,您必须使用getChildCount() 遍历布局,这样您就可以用简单的for 循环替换foreach。
        【解决方案5】:

        最简单的方法是打包所需的字体 与您的应用程序。为此,只需在 项目根目录,并将您的字体(以 TrueType 或 TTF 形式)放入 资产。例如,您可以创建 assets/fonts/ 并将您的 TTF 文件在里面。

        然后,您需要告诉您的小部件使用该字体。很遗憾, 您不能再为此使用布局 XML,因为 XML 不知道 关于您可能作为应用程序资产隐藏的任何字体。 相反,您需要更改 Java 代码,方法是调用 Typeface.createFromAsset(getAssets(), “字体/HandmadeTypewriter.ttf”), 然后获取创建的字体对象并将其传递给您的 TextView 通过 setTypeface()。

        更多参考这里是我得到这个的教程:

        http://www.androidguys.com/2008/08/18/fun-with-fonts/

        【讨论】:

          【解决方案6】:

          我推荐这个approach,在typeface 中添加自定义字体的名称到styles.xml 并将您的字体集放入assets 文件夹中非常好。

          【讨论】:

          • 非常好的方法,我建议注意一下。如果将来无法访问链接,复制主代码并在此处发布也许是个好主意。
          【解决方案7】:

          还有一点除了上述答案。 在片段内使用字体时,字体实例化应在 onAttach 方法(覆盖)中完成,如下所示:

          @Override
          public void onAttach(Activity activity){
              super.onAttach(activity);
              Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/fontname.ttf");
          }
          

          原因
          在片段附加到活动之前有很短的时间。如果在将片段附加到活动之前调用 CreateFromAsset 方法,则会发生错误。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-07-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-03-04
            • 1970-01-01
            • 2016-08-07
            • 2017-09-12
            相关资源
            最近更新 更多