简体   繁体   English

将字体添加到自定义 Android TextView

[英]Add font to custom Android TextView

I could create an Android custom TextView with red background, and I exported it as a Jar file .我可以创建一个带有红色背景的 Android 自定义 TextView,并将其导出为 Jar 文件。 (I followed this link ) (我跟着这个链接

So I added that jar file to an another project and it is fine .所以我将该 jar 文件添加到另一个项目中,它很好。

But when I add a custom font to TextView , that font doesn't exists in exported jar file !但是当我向 TextView 添加自定义字体时,导出的 jar 文件中不存在该字体! So I get an error because android could not find my font .所以我收到一个错误,因为 android 找不到我的字体。

How can I create a full custom component (UI and JAVA) and export it as JAR file?如何创建完整的自定义组件(UI 和 JAVA)并将其导出为 JAR 文件?

if you want to use fonts which are used for example in windows (.TTF) you should do as following:如果您想使用例如在 windows (.TTF)使用的字体,您应该执行以下操作:

1- create a folder named "font" in ASSETS 2- copy your font file there 3- in java activity file: 1- 在 ASSETS 中创建一个名为“font”的文件夹 2- 在那里复制您的字体文件 3- 在 java 活动文件中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Typeface tf=Typeface.createFromAsset(getAssets(), "font/FONTNAME.TTF");
    TextView tv=(TextView)findViewById(R.id.textView1);
    tv.setTypeface(tf);
}

我终于可以创建一个 AAR 并将其添加到项目中。

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

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