【问题标题】:Android: strings resource, multiple strings with the same nameAndroid:字符串资源,多个同名字符串
【发布时间】:2013-08-30 14:35:19
【问题描述】:

在strings.xml中

假设我有以下字符串:

<string name="CourseInfo">Course Information</string>
<string name="CourseInfo1">Course Information:</string>
<string name="CourseInfo2">Course Information:-</string>

你可以看到字符串是一样的,唯一的区别是第二个有一个冒号,第三个有一个冒号和一个破折号。

这是执行此操作的最佳方式吗?这样做似乎有点重复。

【问题讨论】:

  • 在需要时以编程方式进行。 getResources.getString(...) + ":-" ;+ ":"; ?
  • @ShobhitPuri 这是唯一的方法吗?
  • 不知道。让我们等待其他答案。如果有另一种方式,我很高兴知道:)

标签: android string resources repeat


【解决方案1】:

也许你可以使用类似的东西

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

你的例子

<string name="CourseInfo2">Course Information%1$s</string>

然后

Resources res = getResources();
String text = String.format(res.getString(R.string.CourseInfo2), ":");
String text2 = String.format(res.getString(R.string.CourseInfo2), ":-");

【讨论】:

  • 这不考虑其他语言所必需的本地化
猜你喜欢
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-16
  • 1970-01-01
  • 2013-10-23
相关资源
最近更新 更多