gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:06:10 26 4
gpt4 key购买 nike

我有一个 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}。任何帮助都会很棒。

最佳答案

你可以更简单地做到这一点:

android:text= "@{@string/generic_text(profile.name)}"

你的字符串应该是这样的:

<string name="generic_text">My Name is %s</string>

编辑:

  1. 当然,您可以根据需要使用任意数量的变量:

    android:text= "@{@string/generic_text(profile.firstName, profile.secondName)}"

    <string name="generic_text">My Name is %1$s %2$s</string>
  2. 它之所以有效,是因为它是在数据绑定(bind)中设计的。更多文档:https://developer.android.com/topic/libraries/data-binding/expressions#resources

关于android - 如何使用数据绑定(bind)将来自资源的字符串与 XML 中的动态变量结合起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38978499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com