在 XML 中通过数据绑定用一行代码定制字体
2015-10-09 13:50
344 查看
在
XML 中通过数据绑定用一行代码定制字体 (plus.google.com)
Lisa Wray 对新的数据绑定库的能力印象深刻,会在本文中展示如何用它来定制字体
I
started playing with data binding last night for real, and I'm amazed by the power it gives you. Check out this one-liner to set a custom font on a TextView:
@BindingAdapter({"bind:font"})
public static void setFont(TextView textView, String fontName){
textView.setTypeface(Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/" + fontName));
}
In XML:
<TextView
app:font="@{`Source-Sans-Pro-Regular.ttf`}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Previously, this used to take endless lines of repetitive Java code (one for each TextView!). The missing custom typeface support in TextView
has been a grievance held against the Android platform for years.
There's even an inventive library to address it[2]. Data binding makes all of that obselete. If you can't tell, I'm in love!! It's terse, powerful, and one of the biggest leaps forward I've seen for Android productivity.
As usual, you need to put your font file in assets/fonts/, and make sure to include the data binding framework. And if you're doing this
to many TextViews, just get a little more fancy and cache the Typeface instead of creating it every time (thanks for the reminder +Ian
Lake!)
[1] https://developer.android.com/tools/data-binding/guide.html
[2] Calligraphy: https://github.com/chrisjenx/Calligraphy
XML 中通过数据绑定用一行代码定制字体 (plus.google.com)
Lisa Wray 对新的数据绑定库的能力印象深刻,会在本文中展示如何用它来定制字体
I
started playing with data binding last night for real, and I'm amazed by the power it gives you. Check out this one-liner to set a custom font on a TextView:
@BindingAdapter({"bind:font"})
public static void setFont(TextView textView, String fontName){
textView.setTypeface(Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/" + fontName));
}
In XML:
<TextView
app:font="@{`Source-Sans-Pro-Regular.ttf`}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Previously, this used to take endless lines of repetitive Java code (one for each TextView!). The missing custom typeface support in TextView
has been a grievance held against the Android platform for years.
There's even an inventive library to address it[2]. Data binding makes all of that obselete. If you can't tell, I'm in love!! It's terse, powerful, and one of the biggest leaps forward I've seen for Android productivity.
As usual, you need to put your font file in assets/fonts/, and make sure to include the data binding framework. And if you're doing this
to many TextViews, just get a little more fancy and cache the Typeface instead of creating it every time (thanks for the reminder +Ian
Lake!)
[1] https://developer.android.com/tools/data-binding/guide.html
[2] Calligraphy: https://github.com/chrisjenx/Calligraphy
相关文章推荐
- 2.Python标准库_ 时间与日期 (time, datetime包)
- VC++ MFC 常用技巧
- Virtual Studio C++ Version Macro - _MSC_VER
- C/C++---printf/cout 从右至左压栈顺序实例详解
- C#sealed关键字
- php学习基础篇之mysql基础1
- Struts2国际化其二
- 数据处理---Java数据处理之消息队列
- Java IO 之 FileInputStream & FileOutputStream源码分析
- 带哨兵的单链表的操作java版本
- Python 面向对象
- java对象 深度克隆(不实现Cloneable接口)和浅度克隆
- 【转】Java基础:JDK JRE JVM三者之间的联系
- C#中IList与List
- java学习---基础语法
- HashMap和Hashtable的区别
- pymatlab之3D条状图
- PHP DES 加解密
- java实现阿拉伯数字转换为汉字数字源代码
- 小白的第一个百行代码——单链表的增、删、改、查