小编典典

如何使用数据绑定将来自资源的字符串与 XML 中的动态变量结合起来?

all

我有一个 TextView,它有一个硬编码的字符串,我有一个动态变量,我想把它放在这个字符串的末尾。这是我的代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp">
    <TextView
        android:id="@+id/PeopleName"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/Generic_Text"+"@{ Profile.name }" />


</LinearLayout>

我有一个问题 android:text="@string/Generic_Text"+"@{ Profile.name }"。状态“我的Generic_Text名字是”然后Profile.name是动态的并且显然从配置文件到配置文件发生变化。我想要它,以便整个
TextView 输出是 My Name is {Profile.name} 。任何帮助都会很棒。


阅读 59

收藏
2022-07-13

共1个答案

小编典典

你可以这样做:

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

如果您对Generic_Text字符串使用字符串格式。前任。%s在最后

2022-07-13