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包的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章