How do I use databinding to combine a string from resources with a dynamic variable in XML?

前端 未结 9 923
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 13:30

I have a TextView which has a hardcoded string and I have a dynamic variable that I want to put at the end of this string. This is my code:



        
9条回答
  •  既然无缘
    2020-12-07 13:56

    You can do this even simplier:

    android:text= "@{@string/generic_text(profile.name)}"
    

    you string should be like this:

    My Name is %s
    

    Edit:

    1. Of course you can use as many variables as you need:

      android:text= "@{@string/generic_text(profile.firstName, profile.secondName)}"
      
      My Name is %1$s %2$s
      
    2. It works just because it's designed in data binding. More in docs: https://developer.android.com/topic/libraries/data-binding/expressions#resources

提交回复
热议问题