在Android中自定义字体的简单方法:Calligraphy

jopen 9年前

Calligraphy提供在Android中自定义字体的简单方法。screenshot.png

由于现在大多数GitHub工程都是用Gradle构建的,所以工程下提供的Sample不能直接导入Eclipse中,所以按照工程Demo,及工程说明文档,在Eclipse中构建自己的GalligraphySample,以下简要说明构建工程的步骤和需要注意的事项。

1.1下载Galligraphy需要依赖的jar包<calligraphy-1.1.0.jar>

1.2利用Eclipse创建android工程:GalligraphySample

1.3添加自定义字体到工程asserts/,所有的字体定义都和该路径关联

1.4自定义属性,在工程res/values/attrs.xml下添加自定义属性

<?xml version="1.0" encoding="utf-8"?>  <resources>      <attr name="fontPath" format="string"/>  </resources>

1.5在Application类中使用CalligraphyConfig定义默认字体

     1.5.1在工程下创建CalligraphyApplication类

      1.5.2添加如下代码

              @Override

              public void onCreate() {

                        super.onCreate();

                        CalligraphyConfig.initDefault("fonts/Roboto-ThinItalic.ttf", R.attr.fontPath);

               }

       1.5.3在AndroidManifest.xml中application标签下添加属性

                 android:name=".CalligraphyApplication"

1.6在MainActivity中复写attachBaseContext函数    

      @Override

       protected void attachBaseContext(Context newBase) {

        // TODO Auto-generated method stub

        super.attachBaseContext(new CalligraphyContextWrapper(newBase));

       }

构建工程暂时就告一段落了,下面开始使用Calligraphy

2.1在布局文件activity_main.xml中对一个TextView使用特殊字体

<TextView      android:text="@string/hello_world"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      fontPath="fonts/Roboto-Bold.ttf"/>

这里需要注意的是,直接使用fontPath时会报错,这是只需在布局容器中添加属性,例如在LinearLayout标签中添加 tools:ignore="MissingPrefix"

运行程序可以看到加粗效果

2.2在TextAppearance中自定义字体

      在res/values/styles.xml中添加风格

<style name="TextAppearance.FontPath" parent="android:TextAppearance">       <!-- Custom Attr-->      <item name="fontPath">fonts/RobotoCondensed-Regular.ttf</item></style>

布局里直接使用style

<TextView      android:text="@string/hello_world"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:textAppearance="@style/TextAppearance.FontPath"/>

相应的可以在主题theme和styles中自定义字体

<style name="TextViewCustomFont">      <item name="fontPath">fonts/RobotoCondensed-Regular.ttf</item>  </style>
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">      <item name="android:textViewStyle">@style/AppTheme.Widget.TextView</item>  </style>    <style name="AppTheme.Widget"/>    <style name="AppTheme.Widget.TextView" parent="android:Widget.Holo.Light.TextView">      <item name="fontPath">fonts/Roboto-ThinItalic.ttf</item>  </style>

在不同地方定义的字体在使用时候的优先级详细见:

  1. View xml - attr defined here will always take priority.
  2. Style xml - attr defined here is checked next.
  3. TextAppearance xml - attr is checked next, the only caveat to this is IF you have a font defined in theStyle and a TextAttribute defined in the View the Style attribute is picked first!
  4. Theme - if defined this is used.
  5. Default - if defined in the CalligraphyConfig this is used of none of the above are found OR if one of the above returns an invalid font.

OK,描述完毕,在整个过程比较关键也是容易忽视的地方就是在application标签下添加属性 android:name=".CalligraphyApplication",如果少了这个你的效果是出不来的,切记切记!!!

来自:http://blog.csdn.net/mlj1668956679/article/details/38679657


项目主页:http://www.open-open.com/lib/view/home/1414936138715