以编程方式调用通用字符串资源

时间:2018-05-14 09:14:24

标签: android android-resources

strings.xml我有一个资源。

<string name="generic_price_with_rupee_symbol">\u20B9 %s</string>

这就是我在Android数据绑定中所做的完美工作

<TextView
      ...
      android:text="@{@string/generic_price_with_rupee_symbol(item.price)}"
      />

问题:

如何在java代码中使用此资源?因为我不想创造新的资源。

我试过

textView.setText(getString(R.string.generic_price_with_rupee_symbol) + "100");

这会产生错误的结果并打印%s

3 个答案:

答案 0 :(得分:2)

格式化的值应作为参数作为第二个值传递给getString(int, Object..)方法

其中Object...

  

将用于替换的格式参数

所以使用

textView.setText(getString(R.string.generic_price_with_rupee_symbol, "100"));
//                                                                  ^^^

答案 1 :(得分:1)

它应该写成这样 -

textView.setText(getString(R.string.generic_price_with_rupee_symbol, "100"));

从文档中查看String getString (int resId, Object... formatArgs)

答案 2 :(得分:1)

使用以下内容: -

textView.setText(getResources().getString(R.string.generic_price_with_rupee_symbol, "100"));
相关问题