DataBinding: How to get resource by dynamic id?

前端 未结 6 1904
予麋鹿
予麋鹿 2020-12-24 02:12

I know that it is possible to reference resources in layout by their resource id:

android:text=\"@{@string/resourceName}\"

However, I would

6条回答
  •  天涯浪人
    2020-12-24 02:28

    Kotlin version:

    @BindingAdapter("template", "resId")
    fun TextView.setFormattedText(template: String, resId: Int) {
        if (template.isEmpty() || resId == 0) return
        text = template.format(resources.getString(resId))
    }
    

    in xml

    
    

提交回复
热议问题