Android DataBinding 字符串拼接

对于字符串拼接 总是忘记如何实现,特此记录。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>

    <variable
        name="bean"
        type="com.ethanco.threadtest.Bean" />
</data>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text='@{@string/user_name+bean.name}' />
</RelativeLayout>

重点在这里

android:text='@{@string/user_name+bean.name}'  

这里的引号需要使用单引号

注意

特别需要注意的是,字符串的拼接不要用直接的文字表示,比如

android:text='@{"user的name:"+bean.name}'  

这种容易出问题。

并且,DataBinding报错并不会指向错误源,事后很难排除。

故,需采用 @string/xxxx的形式进行字符串的拼接

猜你喜欢

转载自blog.csdn.net/EthanCo/article/details/54571710