1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Calligraphy使在Android中自定义字体变得Easy!!!

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

时间:2023-04-11 21:03:28

相关推荐

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使用特殊字体

<TextViewandroid: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

<TextViewandroid: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>

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

Viewxml - attr defined here will always take priority.Stylexml - attr defined here is checked next.TextAppearancexml - attr is checked next, the only caveat to this isIFyou have a font defined in theStyleand aTextAttributedefined in theViewtheStyleattribute is picked first!Theme- if defined this is used.Default- if defined in theCalligraphyConfigthis is used of none of the above are foundORif one of the above returns an invalid font.

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。