我有一个 Recycle View ,我想对其进行迭代并在每个输入字符串前添加一个项目符号点。
我有以下内容:
值.strings.xml
<string name="skill">• %1$s</string>
配置文件列表适配器:
fun bindSkills(skill: String) {
itemView.recycleSkillItem.text = String.format(Locale(R.string.skill.toString()), ${R.string.skill}, skill)
}
这一遍又一遍地打印相同的 int 而没有要点。
在 kotlin 中格式化字符串的最佳实践是什么?
最佳答案
您应该先了解 Android 的工作原理。 R.string.skill
返回字符串的资源 ID,它是一个 Int
要从资源中获取字符串,您必须使用
context.getString(resId)
它还支持字符串格式化。
context.getString(R.string.skill, skill)
context
可以是扩展 Context
的任何内容,即 Activity
、Fragment
。
要在 Kotlin 中格式化 String
,您可以使用字符串插值。
val world = "World"
val helloWorld = "Hello $world" // Hello World
关于xml - Kotlin:格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46642811/