You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android字符串资源多行文本如何避免行首缩进?

Android String资源多行文本避免行首缩进的方法

有三种实用方法解决这个问题:

  • 使用CDATA标签包裹文本
    CDATA会将内部内容当作纯文本处理,XML解析器不会解析其中的空白字符,你可以在资源文件里自由换行排版,显示时不会出现行首缩进。代码示例:

    <string name="text"><![CDATA[
    This is the first line
    
    This is the second line
    
    ...
    ]]></string>
    
  • 去掉文本行首的手动缩进
    直接在<string>标签内换行写文本,不给每行额外加缩进,虽然XML代码里文本部分不够整齐,但显示时不会有多余缩进。代码示例:

    <string name="text">
    This is the first line
    
    This is the second line
    
    ...
    </string>
    
  • 用HTML <br>标签控制换行
    把所有文本写在一行,用<br>标签分隔换行位置,既能保持XML代码整洁,又能精确控制换行和显示格式。代码示例:

    <string name="text">This is the first line<br><br>This is the second line<br><br>...</string>
    

内容的提问来源于stack exchange,提问作者Ginso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 07:35:18