Android string.xml中的空格以及换行符

本文介绍了在安卓开发中如何在string.xml文件内正确处理和显示空格与换行的方法,使用转义字符实现多行文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安卓开发时用value目录下的string.xml文件保存全局的字符串变量,来方便支持多种语言。

看起来就像这样:

[html]  view plain  copy

 在CODE上查看代码片派生到我的代码片


  1. <string name=“app_name”>AndroidTest</string>  

一行字字符串中需要加入换行和空格怎么办,直接在<string name=”xxx”>和</string>中间添加回车和空格是行不通的。

没办法了,只能转义。

[PS:直接添加回车或导致文本信息无法显示]

查到了XML文件中空格和换行等特殊字符的写法。

空格 (&#x0020;)

Tab  (&#x0009;) 

回车 (&#x000D;) 

换行 (&#x000A;)  

但是按这个输入效果并不理想,空格可以显示,但是换行却无效。

想来想去,直接用C语言经典的“\”直接转义,结果OK了。 ( \t    \n )

有时候,最原始的东西往往是最有效的。

                </div>
System.Xml.XmlException HResult=0x80131940 Message=XML 注释中不能包含“--”,并且“-”不能是最后一个字符。 第 49 行,位置 4。 Source=System.Xml StackTrace: 在 System.Xml.XmlTextReaderImpl.Throw(Exception e) 在 System.Xml.XmlTextReaderImpl.Throw(String res, String arg) 在 System.Xml.XmlTextReaderImpl.ParseCDataOrComment(XmlNodeType type, Int32& outStartPos, Int32& outEndPos) 在 System.Xml.XmlTextReaderImpl.ParseCDataOrComment(XmlNodeType type) 在 System.Xml.XmlTextReaderImpl.ParseComment() 在 System.Xml.XmlTextReaderImpl.ParseElementContent() 在 System.Xml.XmlTextReaderImpl.Read() 在 System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace) 在 System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) 在 System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) 在 System.Xml.XmlDocument.Load(XmlReader reader) 在 System.Xml.XmlDocument.LoadXml(String xml) 在 AutoGenerateConfig.FileParser.FileActions.GetFileXmlDocument(String xmlContent, XmlDocument doc) 在 C:\Users\wangdongchang\source\repos\autogenerateconfig2\AutoConfig\AutoGenerateConfig\FileParser\FileActions.cs 中: 第 99 行 在 AutoGenerateConfig.XmlParser.XmlParser.Xml2Doc(TemplateInfo templateInfo) 在 C:\Users\wangdongchang\source\repos\autogenerateconfig2\AutoConfig\AutoGenerateConfig\FileParser\XmlParser.cs 中: 第 658 行 在 AutoGenerateConfig.XmlParser.XmlParser.ADD(TemplateInfo templateInfo) 在 C:\Users\wangdongchang\source\repos\autogenerateconfig2\AutoConfig\AutoGenerateConfig\FileParser\XmlParser.cs 中: 第 550 行 在 AutoGenerateConfig.XmlParser.XmlParser.<>c__DisplayClass25_0.<LoadXmlFileGroup>b__0() 在 C:\Users\wangdongchang\source\repos\autogenerateconfig2\AutoConfig\AutoGenerateConfig\FileParser\XmlParser.cs 中: 第 506 行 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 在 System.Threading.ThreadHelper.ThreadStart() 此异常最初是在此调用堆栈中引发的: [外部代码] AutoGenerateConfig.FileParser.FileActions.GetFileXmlDocument(string, System.Xml.XmlDocument) (位于 FileActions.cs 中) AutoGenerateConfig.XmlParser.XmlParser.Xml2Doc(AutoGenerateConfig.ConfigManager.TemplateInfo) (位于 XmlParser.cs 中) AutoGenerateConfig.XmlParser.XmlParser.ADD(AutoGenerateConfig.ConfigManager.TemplateInfo) (位于 XmlParser.cs 中) AutoGenerateConfig.XmlParser.XmlParser.LoadXmlFileGroup.AnonymousMethod__0() (位于 XmlParser.cs 中) [外部代码]
最新发布
08-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值