无法将硬编码的字符串值传递给 Kotlin 中的 @BindingAdapter

1 android kotlin android-databinding android-binding-adapter

将我的 Android 应用程序 Java 代码转换为 Kotlin,我使用数据绑定将自定义字体设置为 TextViews。我曾经从 XML 传递字体字符串,如下所示

app:customFont="@{'harmonia-semibold.ttf'}"

将@BindingAdapter 转换为 kotlin 后,上面的行不起作用,并且预期会抛出expr 或 lambda 表达式,出现 '''错误。用 getter 方法替换硬编码的字符串值非常有效。下面是我的绑定适配器,不知道为什么它不采用硬编码字符串

@JvmStatic 
@BindingAdapter("app:customFont")
fun setCustomFont(textView: TextView, font: String) {                       
      textView.typeface = Typeface.createFromAsset(textView.context.assets, font)
}
Run Code Online (Sandbox Code Playgroud)

谢谢

Sem*_*lon 8

看到这个快照我遇到了同样的问题。经过大量的试验和错误,我找到了解决方案。这个解决方案对我有用。在硬编码字符串前后使用 ` 符号。

而不是 app:customFont="@{harmonia-semibold.ttf}"

<EditText
                android:id="@+id/etCNPwd"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/edittext_underline_color"
                android:hint="@string/confirm_new_password"
                android:text="0000000000"
                android:inputType="textPassword"
                android:maxLength="10"
                android:textColor="@color/text_color24"
                android:textSize="@dimen/sp_15"
                bind:setUpPwdLayout="@{`Poppins-Regular.ttf`}"
                bind:addTextChangeListener="@{null}"/>
Run Code Online (Sandbox Code Playgroud)

查看快照并找到解决方案。