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



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

谢谢

请参阅此快照,我遇到了同样的问题。经过大量的试验&错误我找到了解决方案。该解决方案对我有用。使用`硬编码字符串之前和之后的标志。

而不是app:custyfont ="@{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}"/>

请参阅快照并找到解决方案。

最新更新