我在 android 的数据绑定 XML 布局中用 space 添加逗号时遇到问题

i am having problem to add comma with space in data-binding XML layout in android

数据绑定最常用于将布局中的 UI 视图绑定到数据源。这就是我决定使用这种方法的原因。

实际上,我要连接数据源中的值并显示在视图中。连接不是什么大问题我只是在数据源的值或变量之间添加了“+”运算符。

问题是如何在其中两个之间添加space。

通过“+”运算符连接两个值工作正常。


  <TextView
                    android:id="@+id/location"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    tools:text="@sample/title"
                    android:text="@{item.location.get(0).getlocation_text + item.location.get(0).city}"
                    android:maxLines="2"
                    android:ellipsize="end"
                    android:gravity="start"
                    android:textAppearance="@style/TextDesc2"
                    card_view:layout_constraintTop_toBottomOf="@+id/title"
                    card_view:layout_constraintStart_toStartOf="parent"
                    card_view:layout_constraintEnd_toEndOf="parent"
                    android:layout_marginTop="8dp" card_view:layout_constraintHorizontal_bias="0.0"/>

我需要结果 "locatin_text, city"

但现在我得到 "location_textcity"

试试这个

android:text='@{String.format("%s %s", item.location.get(0).getlocation_text, item.location.get(0).city)}'

您可以使用 双引号 反引号 单引号 双引号s。检查 this Android 开发者文档参考。

第一种方式

android:text="@{item.location.get(0).getlocation_text + `, ` +  item.location.get(0).city}"

第二种方式

android:text='@{item.location.get(0).getlocation_text + ", " +  item.location.get(0).city}'