Android中的自定义字体

我已经阅读了一些文章,并在Google上search,但我没有做到。

我的问题是关于字体。

在Android中, "android:typeface"中只有4个属性:Normal,Sans,Serif,Monospace。

那么在我的应用程序中使用“Verdana”我该怎么做?

请build议我在我的Android应用程序中使用这种字体的正确方法。

这是一个简单的例子…在项目的根目录中创build一个名为assets/fonts/的文件夹,然后粘贴TTF字体文件(在本例中为Verdana.ttf)。 然后,如果您想将该字体应用于TextView ,请执行以下操作:

 import android.graphics.Typeface; public class FontSampler extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); TextView tv=(TextView)findViewById(R.id.custom); Typeface face=Typeface.createFromAsset(getAssets(), "fonts/Verdana.ttf"); tv.setTypeface(face); } } 

这个例子是从ComonsWare书(由马克·墨菲写的)取得的。 你可以从GitHub下载完整的例子 。

您可以在https://github.com/neopixl/PixlUI使用PixlUI

导入他们的.jar并在XML中使用它

  <com.neopixl.pixlui.components.textview.TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" pixlui:typeface="GearedSlab.ttf" /> 

您可以使用简单的EasyFonts第三方库来为您的TextView设置各种自定义字体。 通过使用这个库,您不必担心下载并将字体添加到assets / fonts文件夹中。 还有关于字体对象的创build。

这个库不提供Verdana字体的脸。

但提供以下字体。 你可能想用哪个

  • 的Roboto
  • Droid Serif
  • 机器人机器人
  • 自由
  • 有趣的提升者
  • Android国家
  • 绿色鳄梨
  • 承认

只是:

 TextView myTextView = (TextView)findViewById(R.id.myTextView); myTextView.setTypeface(EasyFonts.robotoThin(this)); 

我是这个图书馆的作者。

好!!
这个问题是相当旧的,但如果有人正在寻找答案(在2015年)如何通过XML代码直接应用自定义字体到所有Textviews直接看到如下:

第一
我们需要在您的应用程序目录内的assets文件夹中添加自定义字体:
.ttf.otf都可以在Android上使用

第二:
创build类如下所示扩展TextView的CustomTextView:

 public class CustomTextView extends TextView { public CustomTextView(Context context) { super(context); } public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public void setTypeface(Typeface tf) { super.setTypeface(FontCache.getFont(getContext(),"fonts/<font_name>")); } } 

第三:
在CustomTextView的setTypeface()方法中使用FontCache类。目的是使用HashMap进行基本的字体caching:

 public class FontCache { private static Map<String,Typeface> fontMap = new HashMap<String,Typeface>(); public static Typeface getFont(Context context,String fontname){ if(fontMap.containsKey(fontname)){ return fontMap.get(fontname); } else{ Typeface tf = Typeface.createFromAsset(context.getAssets(),fontname); fontMap.put(fontname,tf); return tf; } } } 

第四: [最后一步]我们现在所做的就是在需要自定义字体textview的xml文件中直接使用CustomTextView:

 <<package_name>.CustomTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Custom Font Text" android:textSize ="18sp" android:textAppearance="?android:textAppearanceSmall" android:id="@+id/custom_txt" /> 

对不起,如果这已经被张贴在SO的某处。 只是想分享,如果它可以帮助别人!

要全局更改您的应用的(自定义)字体,请查看书法

只需将书法添加到您的gradle.build,并将以下片段添加到您的Application.onCreate()

 CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() .setDefaultFontPath("fonts/MyCustomFont.ttf") .setFontAttrId(R.attr.fontPath) .build() ); 

并在每个活动中添加以下内容:

 @Override protected void attachBaseContext(Context newBase) { super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); } 

这就是所有你需要做的全局更改你的应用程序中的字体。 看看文档的更多细节。

 // My example show you how to change fonts into a normal textView or list view create a fonts folder into your assets dir of android and copy your custom font in that .. assets/fonts/monaco.ttf // Font path String fontPath = "fonts/monaco.ttf"; // Loading Font Face Typeface tf = Typeface.createFromAsset(getAssets(), fontPath); // CASE 1 : Inside your list view holder.name = (TextView) convertView .findViewById(R.id.textView_cityName); // set name of text in each row holder.name.setText(CitiesNames.get(position)); // set the type of font you want to set holder.name.setTypeface(tf); // CASE 2 : Inside your text view TextView tx = (TextView)findViewById(R.id.textview1); tx.setTypeface(tf); //vKj 
 TextView textView = (Textview) findViewById(R.id.mytext); Typeface face=Typeface.createFromAsset(getAssets(), "fonts/Verdana.ttf"); textView.setTypeFace(face);