使用DataBinding来进行字体的自定义

来源:互联网 发布:网眼监控软件 编辑:程序博客网 时间:2024/05/21 18:38

1 .通过findViewById找到view,然后一个个的去设置字体

  1. Typeface customFont = Typeface.createFromAsset(this.getAssets(), "fonts/customFont.ttf");
  2. TextView view = (TextView) findViewById(R.id.text);
  3. view.setTypeface(customFont);
  4. ···

一个看着还好,可是应用中可是有很多的文本或者按钮的,不可能逐个去设置。于是大多数都选择了第二种方法。

2 .创建一个子类,继承自TextView或者Button等等

  1. public class CustomTextView extends TextView {
  2. public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
  3. super(context, attrs, defStyle);
  4. }
  5. public CustomTextView(Context context, AttributeSet attrs) {
  6. super(context, attrs);
  7. }
  8. public CustomTextView(Context context) {
  9. super(context);
  10. }
  11. public void setTypeface(Typeface tf, int style) {
  12. if (style == Typeface.BOLD) {
  13. super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/CustomFont_Bold.ttf"));
  14. } else {
  15. super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/CustomFont.ttf"));
  16. }
  17. }
  18. }

然后在xml文件中每次都使用自定义的View,相比上一个方案,这一个要稍微好点。但相信都达不到各位的要求,只不过是换一个字体,需要这么麻烦吗?

3 . Calligraphy

这是一个开源库,地址是https://github.com/chrisjenx/Calligraphy ,在github上5000+star,感觉还是不错的,也从侧面说出自定义字体的需求量有多大。

那么接下来就讲重点了

如何使用DataBinding来进行字体的自定义呢?

TOO EASY,不用每次都去手写,也不需要自定义View

首先,打开DataBinding

  1. android {
  2. compileSdkVersion 25
  3. buildToolsVersion "25.0.0"
  4. defaultConfig {
  5. ···
  6. }
  7. buildTypes {
  8. ···
  9. }
  10. dataBinding {
  11. enabled = true
  12. }
  13. }

再看结构

结构.png

在项目中的assets/fonts文件夹下加入了5种字体,然后在strings.xml中定义了字体的路径

  1. <resources>
  2. <string name="notoSans_regular">fonts/NotoSans-Regular.ttf</string>
  3. <string name="notoSansui_boldItalic">fonts/NotoSansUI-BoldItalic.ttf</string>
  4. <string name="icomoon">fonts/icomoon.ttf</string>
  5. <string name="nutso2">fonts/Nutso2.otf</string>
  6. <string name="ruthie">fonts/Ruthie.ttf</string>
  7. ···
  8. </resources>

而在layout的xml中只需要这么使用,那么便已经完成了8成了

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layout xmlns:android="http://schemas.android.com/apk/res/android">
  3. <data>
  4. </data>
  5. <LinearLayout
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical"
  9. android:gravity="center">
  10. <TextView
  11. android:layout_width="match_parent"
  12. android:layout_height="50dp"
  13. android:gravity="center"
  14. android:text="Hello world"
  15. android:textSize="18sp"
  16. android:typeface="@{@string/nutso2}"/>
  17. <TextView
  18. android:layout_width="match_parent"
  19. android:layout_height="50dp"
  20. android:gravity="center"
  21. android:textSize="18sp"
  22. android:text="Hello world"
  23. android:typeface="@{@string/notoSans_regular}"/>
  24. <TextView
  25. android:layout_width="match_parent"
  26. android:layout_height="50dp"
  27. android:gravity="center"
  28. android:textSize="18sp"
  29. android:text="Hello world"
  30. android:typeface="@{@string/notoSansui_boldItalic}"/>
  31. <TextView
  32. android:layout_width="match_parent"
  33. android:layout_height="50dp"
  34. android:gravity="center"
  35. android:text="Hello world"
  36. android:textSize="18sp"
  37. android:typeface="@{@string/icomoon}"/>
  38. <TextView
  39. android:layout_width="match_parent"
  40. android:layout_height="50dp"
  41. android:gravity="center"
  42. android:text="Hello world"
  43. android:textSize="18sp"
  44. android:typeface="@{@string/ruthie}"/>
  45. </LinearLayout>
  46. </layout>

而剩下的两成,一成在FontBinding文件

  1. public class FontBinding {
  2. @BindingConversion
  3. public static Typeface convertStringToFace(String s){
  4. try {
  5. return Typeface.createFromAsset(FontApp.getInstance().getAssets(),s);
  6. }catch (Exception e){
  7. throw e;
  8. }
  9. }
  10. }

这里定义了一个转换器,将xml文件中获取到的字符资源,转换成了一个Typeface

最后一成,只需要使用就好了

  1. public class MainActivity extends AppCompatActivity {
  2. ActivityMainBinding mMainBinding;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. mMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
  7. }
  8. }

看一下截图,一起哈啤截图.jpg

没了解过DataBinding的人可能不知道ActivityMainBinding是怎么来的,这是编译时生成的,在如下图所示的位置可以找到路径.png打开ActivityMainBinding可以看到这样的代码

  1. this.mboundView1.setTypeface(com.ditclear.sample.FontBinding.convertStringToFace(mboundView1.getResources().getString(R.string.nutso2)));
  2. this.mboundView2.setTypeface(com.ditclear.sample.FontBinding.convertStringToFace(mboundView2.getResources().getString(R.string.notoSans_regular)));
  3. this.mboundView3.setTypeface(com.ditclear.sample.FontBinding.convertStringToFace(mboundView3.getResources().getString(R.string.notoSansui_boldItalic)));
  4. this.mboundView4.setTypeface(com.ditclear.sample.FontBinding.convertStringToFace(mboundView4.getResources().getString(R.string.icomoon)));
  5. this.mboundView5.setTypeface(com.ditclear.sample.FontBinding.convertStringToFace(mboundView5.getResources().getString(R.string.ruthie)));

这些mboundViewX就是xml中的TextView,由于没写id,所以都是自动生成的名称,可以看到给每一个使用了绑定的TextView都设置了字体,相当于系统帮我们做了第一个方法中重复性的工作。

当然我们还可以优化一下,由于每次都要操作assests文件夹,也会带来一定的开销,所以也有必要提供一个字体缓存FontCache,代码来自 britzl on stackoverflow

  1. public class FontCache {
  2. private static ArrayMap<String, Typeface> fontCache = new ArrayMap<String, Typeface>();
  3. public static Typeface getTypeface(String fontName, Context context) {
  4. Typeface tf = fontCache.get(fontName);
  5. if(tf == null) {
  6. try {
  7. tf = Typeface.createFromAsset(context.getAssets(), fontName);
  8. }
  9. catch (Exception e) {
  10. return null;
  11. }
  12. fontCache.put(fontName, tf);
  13. }
  14. return tf;
  15. }
  16. }

那么FontBinding就变成了这样

  1. public class FontBinding {
  2. @BindingConversion
  3. public static Typeface convertStringToFace(String fontName){
  4. try {
  5. return FontCache.getTypeface(fontName,FontApp.getInstance());
  6. }catch (Exception e){
  7. throw e;
  8. }
  9. }
  10. }

补充: 如果不想每次都在xml中设置字体,可以在绑定一个常用的属性时设置一个默认的字体。比如字体是需要作用在text上的,那么我们只需要在setText的时候绑定一个默认的字体就

原创粉丝点击