Calligraphy使在Android中自定义字体变得Easy!!!

                     Calligraphy使在Android中自定义字体变得Easy!!!

Calligraphy GitHub工程地址

由于现在大多数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",如果少了这个你的效果是出不来的,切记切记!!!

当然相信你是不会忘记你引入你下载的那个jar包的。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值