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

前端 未结 9 921
爱一瞬间的悲伤
爱一瞬间的悲伤 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:55

    In case if you want to type text in XML, you can use `` quotation.

    android:text="@{`Device Name`}"
    

    elsewhere you need to Concat with the String or variable, you can use

    android:text="@{`Device Name`.concat(android.os.Build.MANUFACTURER)}"
    

    if you want to Concat string resource instead of the variable you can do,

    android:text="@{@string/app_name.concat(`Device Name`)}"
    
    0 讨论(0)
  • 2020-12-07 13:56

    You can do this even simplier:

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

    you string should be like this:

    <string name="generic_text">My Name is %s</string>
    

    Edit:

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

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

    0 讨论(0)
  • 2020-12-07 13:58

    Use a Binding Adapter.

    This sample is written in Kotlin and takes into account that the bound variable can be null:

    @BindingAdapter("my_name")
    fun TextView.setMyName(name: String?) {
        this.text =
            if (name.isNullOrEmpty()) "" else "${this.context.getString(R.string.Generic_Text)} $name"
    }
    

    then use the binding adapter in your XML instead of the android:text property

    app:my_name="@{Profile.name}"
    
    0 讨论(0)
  • 2020-12-07 13:59

    You can do this:

    android:text= "@{String.format(@string/Generic_Text, Profile.name)}"
    

    if you use string formatting for your Generic_Text string. ex. %s at the end

    0 讨论(0)
  • 2020-12-07 14:11

    Many ways to concat strings

    1. Using string resource (Recommended because Localization)

    android:text= "@{@string/generic_name(user.name)}"

    Just make string resource like this.

    <string name="generic_name">Hello %s</string>
    

    2. Hard coded concat

    android:text="@{`Hello ` + user.name}"/>
    

    This is useful when you need hardcoded append like + for phone number.

    3. Using String's concat method

    android:text="@{user.firstName.concat(@string/space).concat(user.lastName)}"
    

    Here space is an html entity which is placed inside strings.xml. Because XML does not accept Html entities or special characters directly. (Link Html Entities)

    <string name="space">\u0020</string>
    

    4. Using String.format()

    android:text= "@{String.format(@string/Hello, user.name)}"
    

    you have to import String class in layout in this type.

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android">
        <data>
            <import type="String" />
        </data>
        <TextView
            android:text= "@{String.format(@string/Hello, user.name)}"
            ... >
        </TextView>
    </layout>
    

    5. concat two strings by string resource.

    android:text="@{@string/generic_name(user.firstName,user.lastName)}"
    

    In this case put a string resource in strings.xml

    <string name="generic_name">%1$s, %2$s</string>
    

    There can be many other ways, choose one you need.

    0 讨论(0)
  • 2020-12-07 14:14

    You can also set string resource as parameter to other string resource using formatter like below:

    <string name="first_param_text">Hello</string>
    <string name="second_param_text">World</string>
    <string name="formatted_text">%s lovely %s</string>
    

    and

    android:text="@{String.format(@string/formatted_text, @string/first_param_text, @string/second_param_text)}"
    

    "Hello lovely World" will appear on the view.

    0 讨论(0)
提交回复
热议问题