图标开头的富文本突然右对齐了:HarmonyOS 7 修好的 ParagraphStyle,可能暴露旧布局补丁
·
图标开头的富文本突然右对齐了:HarmonyOS 7 修好的 ParagraphStyle,可能暴露旧布局补丁
消息卡片用一个小图标开头,后面跟金额和说明。项目原来给整段设置 TextAlign.End,但旧版本首个占位是 ImageAttachment 时不生效,于是又在外层加了右对齐补丁。升级 API 26 后,段落样式终于生效,外层补丁也还在,内容被推得比预期更远。
这不是 ParagraphStyle “又坏了”,而是平台修复让历史补偿代码开始重复生效。受影响的不只图片,CustomSpan 作为段落首个占位时也要检查。
验证边界:本文依据华为 2026-08-19 更新的 HarmonyOS 7(API 26)Beta1 行为变更说明。示例中的判定与状态模型已在宿主 Node.js 环境跑过断言;平台组件接入片段未在 API 26 SDK 或真机编译运行,不能替代目标设备验收。

哪个条件同时满足才需要重点回归
| 条件 | 是否关键 | 说明 |
| target >= 26 | 是 | 行为按版本隔离 |
| 首个占位是 ImageAttachment/CustomSpan | 是 | 普通文本开头不是这次核心 |
| 段落设置 ParagraphStyle | 是 | 对齐、缩进等会显现 |
| 外层曾做补偿 | 高风险 | 新旧效果可能叠加 |
案例一:支付摘要以图标开头
先把“是否可能叠加补偿”做成可测试规则,再决定删除哪一层样式。
type RichBlock = { target: number; first: 'text'|'image'|'custom'; paragraphAlign?: string; outerPatch: boolean };
function risk(block: RichBlock): string {
const fixed = block.target >= 26 && block.first !== 'text' && !!block.paragraphAlign;
if (fixed && block.outerPatch) return 'double-compensation';
if (!fixed && block.first !== 'text') return 'legacy-no-effect';
return 'normal';
}
if (risk({target:26,first:'image',paragraphAlign:'end',outerPatch:true}) !== 'double-compensation') {
throw new Error('risk model failed');
}
平台接入中仍应通过 MutableStyledString 插入 ImageAttachment,并给对应段落设置 PARAGRAPH_STYLE。上面的函数只负责迁移审计,不替代 ArkUI 渲染验证。
案例二:CustomSpan 画状态标签
自绘标签常带自己的宽度和绘制逻辑。旧版本为了模拟段落缩进,开发者可能在 CustomSpan 内部多留空白。API 26 后段落样式生效,内部空白就会成为第二份缩进。
type Migration = { removeInnerPadding: boolean; keepParagraphStyle: boolean };
function plan(target: number, firstIsPlaceholder: boolean, hadManualPadding: boolean): Migration {
return {
removeInnerPadding: target >= 26 && firstIsPlaceholder && hadManualPadding,
keepParagraphStyle: true
};
}
const result = plan(26, true, true);
if (!result.removeInnerPadding || !result.keepParagraphStyle) throw new Error('migration plan');
为什么不能简单删除所有外层对齐
同一组件可能同时运行在不同 target 构建、不同首位内容和多语言文本下。应先通过截图或布局断言确认是哪一层负责对齐,再删补丁。无障碍朗读顺序、复制范围和拖拽内容也要一起回归,不能只看视觉位置。
建议的验收矩阵
- 首位普通文本、ImageAttachment、CustomSpan 各一组。
- TextAlign.Start、Center、End 分别截图。
- 中英文、超长文本和字体放大场景。
- 有无历史 padding/外层布局补偿各一组。
- 真机补测复制、拖拽和无障碍焦点。
官方依据
更多推荐

所有评论(0)