如何将外部字体添加到Android应用程序

发布于 2024-10-31 18:30:40 字数 78 浏览 1 评论 0原文

我正在为我的 Android 应用程序寻找一些时尚的字体。但问题是我怎样才能让我的Android应用程序支持外部字体。

谢谢。

I was looking for some stylish fonts for my android application. but the problem is how can i make my android application supportable for external fonts.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

娜些时光,永不杰束 2024-11-07 18:30:41

要实现,您需要使用 Typeface 来完成下面的示例

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);
      }
   }
}

To implement you need use Typeface go through with sample below

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);
      }
   }
}
墨小墨 2024-11-07 18:30:41

实现此目的的最简单方法是打包所需的字体
与您的申请。为此,只需在中创建一个 asset/ 文件夹
项目根目录,并将字体(TrueType 或 TTF 形式)放入
资产。例如,您可以创建资产/字体/并将您的
里面有 TTF 文件。

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

有关更多参考,这里是我得到的教程:

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

The easiest way to accomplish this is to package the desired font(s)
with your application. To do this, simply create an assets/ folder in
the project root, and put your fonts (in TrueType, or TTF, form) in
the assets. You might, for example, create assets/fonts/ and put your
TTF files in there.

Then, you need to tell your widgets to use that font. Unfortunately,
you can no longer use layout XML for this, since the XML does not know
about any fonts you may have tucked away as an application asset.
Instead, you need to make the change in Java code, by calling
Typeface.createFromAsset(getAssets(), “fonts/HandmadeTypewriter.ttf”),
then taking the created Typeface object and passing it to your
TextView via setTypeface().

For more reference here is the tutorial where I got this:

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

难以启齿的温柔 2024-11-07 18:30:41

我推荐这种方法,在字体 到 styles.xml 并将您的字体集放入 assets 文件夹中。

I recommend this approach it very nice with adding name of custom font in typeface to styles.xml and putting your set of fonts into assets folder.

怪异←思 2024-11-07 18:30:41

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

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

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

One more point in addition to the above answers.
When using a font inside a fragment, the typeface instantiation should be done in the onAttach method ( override ) as given below:

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

Reason:
There is a short span of time before a fragment is attached to an activity. If CreateFromAsset method is called before attaching fragment to an activity an error occurs.

薄荷梦 2024-11-07 18:30:40

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

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

请注意,并非所有 TTF 都可以工作。当我进行试验时,它仅适用于一个子集(在 Windows 上,名称以小写字母书写的子集)。

You need to create fonts folder under assets folder in your project and put your TTF into it. Then in your Activity onCreate()

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

Please note that not all TTF will work. While I was experimenting, it worked just for a subset (on Windows the ones whose name is written in small caps).

不打扰别人 2024-11-07 18:30:40

您可以将自定义 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*/);
       }
   }
}

You can use the custom TextView for whole app with custom font here is an example for that

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*/);
       }
   }
}
ゝ杯具 2024-11-07 18:30:40

在资产文件夹中创建一个名为 fonts 的文件夹,然后添加以下链接中的代码片段。

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

Create a folder named fonts in the assets folder and add the snippet from the below link.

Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/fontname.ttf");
textview.setTypeface(tf);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文