我有一个带有文本的简单按钮
如果在 android:text 中我引用字符串,它就会按预期工作。
<Button
android:id="@+id/true_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button" /> // OK
<Button
android:id="@+id/true_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@id/R.string.true_button" /> // doesn`t output anything
但是,如果我尝试通过 R.string."name of string"引用我的字符串,则不会发生任何事情
请解释一下我错过了什么......
最佳答案
您在这里做错了一些事情。要引用 XML 中的字符串,您应该使用
android:text="@string/string_name"
@string 指的是 string.xml 文件,string_name 是您声明的名称。
这就是 string.xml 中所需的行的样子
<string name="string_name">This is a string you are referencing!r</string>
此外,我从未尝试过使用 .分隔词。这可能会导致错误,但我不能 100% 确定。
编辑: michal.z 说你不能引用 R. 或 android.R,这是非常正确的。来自 XML 的资源。仅当您尝试以编程方式引用资源时才使用它们。
关于java - 通过 R.string 引用字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27350997/