1

将我的 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)
}

谢谢

4

1 回答 1

9

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

而不是 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}"/>

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

于 2018-07-18T08:46:48.727 回答