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

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