摘要:本文将带你了解Android编程之Calligraphy:Android 自定义字体库,希望本文对大家学Android有所帮助
Calligraphy是android 自定义字体库
添加依赖
Download from Maven Central (.jar)
OR
Java
dependencies {
compile 'uk.co.chrisjenx:calligraphy:1.2.0'
}
123
dependencies { compile 'uk.co.chrisjenx:calligraphy:1.2.0'}
使用
字体
把你的所有的自定义字体都添加到assets/fonts 目录下, 所有的字体引用的都是这个目录
自定义属性
我们没有在Jar包中打包 R.attr,所以你需要自己添加你自己的Attr,下面是一个例子 res/values/attrs.xml
Java
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="fontPath" format="string"/>
</resources>
1234
<?xml version="1.0" encoding="utf-8"?><resources> <attr name="fontPath" format="string"/></resources>
配置
在Application类中,使用 CalligraphyConfig定义你默认的字体, 不幸的是Activity#onCreate(Bundle) 在 Activity#attachBaseContext(Context)之后调用, 所以这个配置文件要在之前定义
Java
protected void onCreate() {
super.onCreate();
CalligraphyConfig.initDefault("fonts/Roboto-Regular.ttf", R.attr.fontPath);
//....
}
12345
protected void onCreate() { super.onCreate(); CalligraphyConfig.initDefault("fonts/Roboto-Regular.ttf", R.attr.fontPath); //....}
注入到 Context
Java
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(new CalligraphyContextWrapper(newBase));
}
1234
@Overrideprotected void attachBaseContext(Context newBase) { super.attachBaseContext(new CalligraphyContextWrapper(newBase));}
给TextView自定义字体
Java
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fontPath="fonts/Roboto-Bold.ttf"/>
12345
<TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" fontPath="fonts/Roboto-Bold.ttf"/>
在TextAppearance中自定义字体
Java
<style name="TextAppearance.FontPath" parent="android:TextAppearance">
<!-- Custom Attr-->
<item name="fontPath">fonts/RobotoCondensed-Regular.ttf</item>
</style>
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.FontPath"/>
123456789
<style name="TextAppearance.FontPath" parent="android:TextAppearance"> <!-- Custom Attr--> <item name="fontPath">fonts/RobotoCondensed-Regular.ttf</item></style><TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="@style/TextAppearance.FontPath"/>
在styles中自定义字体
Java
<style name="TextViewCustomFont">
<item name="fontPath">fonts/RobotoCondensed-Regular.ttf</item>
</style>
123
<style name="TextViewCustomFont"> <item name="fontPath">fonts/RobotoCondensed-Regular.ttf</item></style>
Custom font defined in Theme
Java
<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>
<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>
开源地址:https://github.com/chrisjenx/Calligraphy
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号