Android:dataBinding无法直接使用中文字符的值

<TextView
     android:id="@+id/text4"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginStart="16dp"
     android:layout_marginTop="20dp"
     android:textColor="@color/black_33"
     android:textSize="15sp"
     android:textStyle="bold"
     android:text="@{`x`}"
     app:layout_constraintStart_toStartOf="parent"
     app:layout_constraintTop_toBottomOf="@+id/rg_result" />

直接使用x这是可以的,这里只是打个比方,正常使用的话,一般使用三元运算,比如日期为空的话,我是这样写的

@{date == null ? `x`:DateUtils.getHorizontalDate(date)}

但是把x替换成 开始时间 之类的中文就会失败。

解决办法:

1、把这个中文在引入的ViewModel写成常量变量的形式,然后布局文件使用,那么就是@{viewModel.x} 这样是可以的

2、把这个中文写入strings.xml中,使用时@{@string/start_time

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章