我需要在 TextView
中显示一个字符串,该字符串以 Twitter 句柄的 @ 符号开头。当我将字符串放入我的 strings.xml 时,它不喜欢字符串开头的 @ 并给出错误:
error: Error: No resource type specified (at 'twitter_handle' with value '@twitter_handle').
其中包含使用字符串资源的 TextView
的 layout.xml 给出了错误:
The following classes could not be found:
- TextView (Change to android.widget.TextView, Fix Build Path, Edit XML)
如果 @ 符号不在文本的开头,而是在字符串的其他任何位置,则没有错误。
如果我使用 @
或 \u0040
转义 @ 符号,则错误会在 strings.xml 中消失,但我仍然会在布局.xml。
有没有办法在 layout.xml 的 TextView
中使用以 @ 符号开头的字符串?
字符串.xml:
...
<string name="twitter_handle">@twitter_handle</string>
...
布局.xml:
...
<TextView
...
android:text="@string/twitter_handle"
... />
...
毫无疑问,问题与这样一个事实有关,即前导 @ 暗示对资源的引用,但不应该考虑转义的 @ 符号并使其正常工作吗?
更新:这个问题被发现是 ADT Eclipse 插件中的错误。安装最近发布的最新版本修复了该问题,转义 strings.xml 中的 @ 不再导致错误。
最佳答案
用一个前导斜杠转义它,像这样:<string name="test">\@twitter</string>
编辑:我应该读过。您可以以编程方式分配 String
到 TextView
, 通过从中获取一个字符串并将其设置为 TextView
String twitter = getString(R.id.twitter);
textView.setText(twitter);
编辑 2:试试这个而不是 @ 符号:@
.这是标志的 html 代码。
关于android - 在 TextView 的开头显示 @,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14491674/