如Lisa Wray 的一篇文章中所述,我正在尝试将一些自定义字体应用于我TextView
的一行。是进入的项目的一部分TextView
RecyclerView
我已将数据绑定依赖项添加到我的顶级构建文件中。
classpath 'com.android.tools.build:gradle:1.3.0'
classpath "com.android.databinding:dataBinder:1.0-rc1"
我还将插件应用到我的主模块:
apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'
这是item.xml
将添加到RecyclerView
.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools">
<data></data>
<android.support.v7.widget.CardView
android:id="@+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="100dp"
android:layout_height="130dp"
android:layout_gravity="center"
card_view:cardCornerRadius="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="100dp"/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:font="@{@string/font_yekan}"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</layout>
我添加了一个layout
根元素并app:font="@{@string/font_yekan}"
结合了一个静态 setter 方法:
@BindingAdapter({"bind:font"})
public static void setFont(TextView textView, String fontName) {
textView.setTypeface(Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/" + fontName));
}
应该做的伎俩。但是当我运行程序时,字体没有改变。但是,当我删除上述静态方法时,出现以下错误:
找不到参数类型为 java.lang.String 的属性“app:font”的设置器。
所以数据绑定框架已经识别了绑定的东西,但是 setter 方法没有被调用(日志不打印输出)。
这里有什么问题?