在 XML 中通过数据绑定用一行代码定制字体
来源:互联网 发布:win7软件停止运行 编辑:程序博客网 时间:2024/04/28 12:20
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 中通过数据绑定用一行代码定制字体
- 在ASP.NET中绑定XML数据
- 2种在HTML中绑定XML数据的方法
- 如何在绑定数据的DropDownList下拉菜单中第一行添加空白项?
- WPF学习笔记 - 数据绑定(在代码中)
- Android中通过代码获取arrays.xml文件中的数据
- Android中通过代码获取arrays.xml文件中的数据
- Android中通过代码获取arrays.xml文件中的数据
- 在table中通过按钮添加一行或者删除一行
- 在代码中,通过资源XML获取数组
- 在已绑定数据源的combobox中增加一行
- C#中通过反射进行数据绑定
- 在Dom4j中使用xpath--查看xml数据代码
- paip.获取proxool的配置 xml读取通过jdk xml 初始化c3c0在代码中总结
- 用CSS让字体在一行内显示不换行
- 用CSS让字体在一行内显示不换行
- 用CSS让字体在一行内显示不换行
- 用CSS让字体在一行内显示不换行
- VC++ MFC 常用技巧
- OC基础笔记
- redis php admin
- netty TCP server心跳机制
- ffmpeg 使用滤镜
- 在 XML 中通过数据绑定用一行代码定制字体
- 初识移动端跨平台(JavaScript篇)
- Dubbo架构设计详解
- 教主Baby大婚能收多少份子钱?
- Reading and Writing using a Large Random Access File
- 高度平衡
- 成为核心程序员的一些建议
- 如何在lua中打印一个数组(table)
- PHP CLI 模式详解