使用 Android-M 数据绑定应用的单行字体不起作用

One-line font applying using Android-M data-binding doesn't work

我正在尝试使用 a post by Lisa Wray 中所述的一行将一些自定义字体应用到我的 TextViewTextView 是进入 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'

这是将添加到 RecyclerViewitem.xml 文件。

<?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));
}

应该可以解决问题。但是当我 运行 程序时,字体没有改变。 但是,当我删除上面的静态方法时,出现以下错误:

Cannot find the setter for attribute 'app:font' with parameter type java.lang.String.

所以数据绑定框架已经识别了绑定内容,但是 setter 方法没有被调用(日志不打印输出)。

这里有什么问题?

提供上述布局和设置,假设如下:

在您的 RecyclerView 适配器中,您已通过以下方式之一绑定视图:

  1. 在您的适配器的 onCreateViewHolder 方法中 class

    @Override
    public MyAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.recycler_item,
                parent, false);
        return new MyHolder(binding.getRoot());
    }
    
  2. 或者在其onBindViewHolder方法中

        @Override
        public void onBindViewHolder(MyAdapter.MyHolder holder, int position) {
            DataBindingUtil.bind(holder.itemView);
            //...
        }
    

以下资源设置

您的资产文件夹应与此类似:

您的字符串资源文件应具有字体的完整限定名称:

<string name="kenyan">kenyan_rg.ttf</string>

有了这个保证,它应该可以工作(对我来说是这样)

    Even i have the same problem below is my code


    @BindingAdapter({"bind:customFont"})
        public static void customFont(TextView textView, String fontName) {
textView.setTypeface(Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/"+fontName));
        }    

    <TextView
                android:id="@+id/tv_book_stay"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="35dp"
                app:customFont="@{@string/roboto_bold}"
                android:text="@{data.mob1BookYourStay}"
                android:textSize="22sp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/iv_logo" />

    <string name="roboto_bold">roboto_bold.ttf</string>

    DataBindingUtil.setContentView(this, R.layout.activity_home);