如何在TextView中显示双引号(")符号?

129

我试图在XML文件的TextView中显示一些用双引号括起来的文字,但是它不起作用。

<TextView 
style="@style/TextStyle" 
android:text="message "quote string 1" and "quote string 2" end message" 
android:id="@+id/lblAboutPara3" 
android:autoLink="web"/>

有谁知道这个问题的解决方案吗?

9个回答

250

strings.xml 文件中,你可以使用反斜杠转义特殊字符(例如双引号):

"message \"quote string 1\" and \"quote string 2\" end message"

但在视图XML中(例如layout.xml),您必须使用HTML字符实体(如&quot;):

"message &quot;quote string 1&quot; and &quot;quote string 2&quot; end message"

更多信息请访问http://developer.android.com/guide/topics/resources/string-resource.html


7
它可以在strings.xml文件中使用<string name="double_quote">"</string>,但在layout.xml文件中无法使用。 - Tyler Davis
这个答案是不正确的,因为OP明确询问的是“在xml文件的文本视图中的双引号”,而不是在strings.xml中。 - Patrick

81

使用&quot;符号来解决这个难题 :)

android:text="message &quot;quote string 1&quot;" 

1
如果您想在布局文件中使用“**"”符号而不使用string.xml**,则必须正确。 - Jaydipsinh Zala
4
你不能在 strings.xml 中使用这个,这是一个大问题。 - Mike Baxter

16

使用 转义字符。要显示双引号,请使用 \"

你的代码将是:

android:text="message \"quote string 1\" and "quote string 2\" end message" 

11
请尝试。
<TextView 
style="@style/TextStyle" 
android:text='message \"quote string 1\" and \"quote string 2\" end message' 
android:id="@+id/lblAboutPara3" 
android:autoLink="web"/> 

10

9

如果您的字符串中有双引号,必须对其进行转义(\")。用单引号括起字符串无效。

在strings.xml文件中

<string name="good_example">This is a \"good string\".</string>

来源http://developer.android.com/guide/topics/resources/string-resource.html

在 Android 应用程序中,字符串资源是一种非常有用的机制,可以让你轻松地管理应用程序中的所有文本。通过将所有文本存储在一个集中的位置,你可以更容易地维护和更新应用程序的文本。

使用字符串资源还可以使你的应用程序更容易国际化(支持不同语言和地区)。通过提供不同语言的字符串资源,你的应用程序可以在不同的语言环境下正确地显示文本。

要使用字符串资源,请将所有文本添加到 res/values/strings.xml 文件中。然后,在代码中引用该字符串,而不是直接写入文本。这样做可以使你的代码更加模块化和易于维护。


8
TextView.setText(Html.fromHtml("&ldquo; " + "YOUR TEXT" + " &rdquo;"));

6
<TextView 
style="@style/TextStyle" 
android:text='message "quote string 1" and "quote string 2" end message' 
android:id="@+id/lblAboutPara3" 
android:autoLink="web"/> 

1
使用单引号将消息包装起来,您可以在字符串内部使用任意多的双引号。
android:text='message "quote string 1" and "quote string 2" end message'

1
使用 onClick 也完美运作。 - amc software

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接