
前言
Android 13 向 TextView 控件引入了新的换行策略,同时针对日文提供了换行优化。系统将依据开发者指定的换行策略、日文短语换行策略进行文本换行。这将促使文本内容不再杂乱无章、更加具有层次、便于阅读。
下面我们从 API、实战、适配办法等多个角度来快速学习一下这个新特性。
换行策略 lineBreakStyle
Indicates the line break strategies can be used when calculating the text wrapping.
| Constant | Value | Description |
|---|---|---|
| none | 0 | No line break style specific. |
| loose | 1 | Use the least restrictive rule for line-breaking. |
| normal | 2 | Indicate breaking text with the most comment set of line-breaking rules. |
| strict | 3 | ndicates breaking text with the most strictest line-breaking rules. |
事实上来自于 CSS 的 line-break 属性,主要是针对 CJK 语言,也就是中日韩 3 种语言提供的换行规则。
Android 提供的属性和 CSS 的略有差异,但 loose、normal 和 strict 这三个属性进行了保留,后面将通过 DEMO 进行属性表现上的差异说明。
短语换行策略 lineBreakWordStyle
Specify the phrase-based line break can be used when calculating the text wrapping.
| Constant | Value | Description |
|---|---|---|
| none | 0 | No line break word style specific. |
| phrase | 1 | Specify the phrase based breaking. |
默认不生效,设置为 phrase 则生效。
动态设置
除了上述属性以外,TextView 理所应当地提供了 setLineBreakConfig() 来动态更新换行策略。
public void setLineBreakConfig (LineBreakConfig lineBreakConfig)
| Parameters | |
|---|---|
lineBreakConfig |
LineBreakConfig: the line break config for text wrapping. This value cannot be null. |
LineBreakConfig
Android 13 新引入的承载换行策略的配置类,提供了上述属性相对应的常量以及返回策略所必要的 get 方法。
| Constants | |
|---|---|
int |
LINE_BREAK_STYLE_LOOSEUse the least restrictive rule for line-breaking. |
int |
LINE_BREAK_STYLE_NONENo line break style specified. |
int |
LINE_BREAK_STYLE_NORMALIndicate breaking text with the most comment set of line-breaking rules. |
int< |

Android13为TextView引入了新的换行策略和日文短语换行策略,提供了更好的文本布局和阅读体验。开发者可以通过LineBreakConfig动态设置lineBreakStyle和lineBreakWordStyle,影响文本换行规则。在日语环境下,短语换行策略可优化阅读连贯性,且性能影响微乎其微。适配时需注意布局和已有换行设置,确保兼容性。
最低0.47元/天 解锁文章
6393

被折叠的 条评论
为什么被折叠?



