【鸿蒙开发】pattern 的使用
当开发应用的时候,我们会对一些布局的风格有统一的设计。例如多Text组件组成的表单界面,我们会将字体的大小、颜色等等外观设置一致,其中有大量的重复XML代码。而修改其中一个共同属性的时候(例如字体大小)又需要逐一的去修改每个Text组件。这个时候pattern就排上用场了,先看实例。
例如我们要完成下面的界面:

下面的是在使用pattern之前的布局,其中大量的重复字段,维护起来也很复杂
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:orientation="vertical"
ohos:total_weight="8"
ohos:width="match_parent">
<Text
ohos:background_element="$graphic:bg_example_pattern"
ohos:height="0"
ohos:margin="18vp"
ohos:text="条目一"
ohos:text_alignment="center"
ohos:text_color="#FFFFFF"
ohos:text_size="32fp"
ohos:weight="1"
ohos:width="match_parent"/>
<Text
ohos:background_element="$graphic:bg_example_pattern"
ohos:height="0"
ohos:margin="18vp"
ohos:text="条目二"
ohos:text_alignment="center"
ohos:text_color="#FFFFFF"
ohos:text_size="32fp"
ohos:weight="1"
ohos:width="match_parent"/>
<Text
ohos:background_element="$graphic:bg_example_pattern"
ohos:height="0"
ohos:margin="18vp"
ohos:text="条目三"
ohos:text_alignment="center"
ohos:text_color="#FFFFFF"
ohos:text_size="32fp"
ohos:weight="1"
ohos:width="match_parent"/>
<Text
ohos:background_element="$graphic:bg_example_pattern"
ohos:height="0"
ohos:margin="18vp"
ohos:text="条目四"
ohos:text_alignment="center"
ohos:text_color="#FFFFFF"
ohos:text_size="32fp"
ohos:weight="1"
ohos:width="match_parent"/>
<Text
ohos:background_element="$graphic:bg_example_pattern"
ohos:height="0"
ohos:margin="18vp"
ohos:text="条目五"
ohos:text_alignment="center"
ohos:text_color="#FFFFFF"
ohos:text_size="32fp"
ohos:weight="1"
ohos:width="match_parent"/>
</DirectionalLayout>
下面是使用pattern后的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:orientation="vertical"
ohos:total_weight="8"
ohos:width="match_parent">
<Text
ohos:height="0"
ohos:text="条目一"
ohos:text_alignment="center"
ohos:theme="$pattern:pattern_example"
ohos:width="match_parent"/>
<Text
ohos:height="0"
ohos:text="条目二"
ohos:text_alignment="center"
ohos:theme="$pattern:pattern_example"
ohos:width="match_parent"/>
<Text
ohos:height="0"
ohos:text="条目三"
ohos:text_alignment="center"
ohos:theme="$pattern:pattern_example"
ohos:width="match_parent"/>
<Text
ohos:height="0"
ohos:text="条目四"
ohos:text_alignment="center"
ohos:theme="$pattern:pattern_example"
ohos:width="match_parent"/>
<Text
ohos:height="0"
ohos:text="条目五"
ohos:text_alignment="center"
ohos:theme="$pattern:pattern_example"
ohos:width="match_parent"/>
</DirectionalLayout>
pattern.json 部分代码
{
"pattern": [
{
"name": "pattern_example",
"value": [
{
"name": "background_element",
"value": "$graphic:bg_example_pattern"
},
{
"name": "margin",
"value": "18vp"
},
{
"name": "text_color",
"value": "#FFFFFF"
},
{
"name": "text_size",
"value": "32fp"
},
{
"name": "weight",
"value": "1"
}
]
}
]
}
使用pattern后我们只需要在pattern中修改对应的属性就会全部应用到引用该配置的组件。对于后期的维护也大大的提升了效率,不再需要逐一修改(而且可能存在漏改的组件)。
相关推荐
HarmonyOS智能穿戴应用设计文档
为构建一致且易用的操作体验,我们总结了这份设计指南对应提供导适于智能穿戴设备,此设计指南包括设计原则、系统架构、应用架构、人机交互、视觉风格、控件等内容。
LcaiTipsDialog.har
LcaiTipsDialog 是一套基于 HarmonyOS ArkTS 语言开发的轻量级、高可定制化的自定义弹窗组件库。它采用链式调用(Builder Pattern)的设计模式,极大地简化了鸿蒙应用中常见提示框、确认框、图片展示及富文本弹窗的开发流程。该组件库支持多种显示模式,内置了分段高亮文本解析能力,并提供了完善的交互回调机制,旨在为开发者提供一致且优雅的 UI 交互体验
harmonyos2-elements-of-rust:Rust风格和哲学
和声2 :fire: Rust 编程技巧 :fire: 一组软件工程技术,用于使用 Rust 有效表达意图。 清理 本节是关于提高清晰度。 对抗向右的压力 在与编译器争吵之后,后退并看到几个嵌套的组合器链或匹配语句并不罕见。 编写干净的 Rust 的大部分艺术与去嵌套技术的明智应用有关。 去嵌套的基础知识 使用? 扁平化错误处理,但注意不要将错误转换为顶级枚举,除非在代码中的同一点处理它们是有意义的。 将不同的关注点放在不同的类型中。 当组合链超过一条线时,将它们分开。 为中间步骤指定有用的名称。 在许多情况下,多行组合器链可以更清楚地改写为 for 循环。 完整复杂类型的模式匹配,而不是使用嵌套的匹配语句 如果您的 match 语句只有一个您关心的模式,后跟一个通配符,请将 match 语句替换为if let My(Match(Pattern(thing))) = matched_thing { /*...*/ }可能与如果您之前关心通配符,则可以使用else分支。 运行货物剪辑! 它可以为清理代码提供许多合法有用的建议 元组匹配 如果您发现自己编写的代码如下所示: let a = Some ( 5 )
harmonyos2-fluent-schemer:我写了这个,所以我可以在不痛苦的情况下验证东西
和声2 流利的schemer const librarySchema = object ( { dependenciesCount : number ( ) . min ( 0 ) . max ( 10 ) . integer ( ) . optional ( ) , name : string ( ) . minlength ( 2 ) . maxlength ( 10 ) , testCoverage : number ( ) . min ( 0 ) . max ( 100 ) . optional ( ) , lastCommitDate : date ( ) . after ( new Date ( new Date ( ) . setMonth ( new Date ( ) . getMonth ( ) - 1 ) ) ) , contributors : array ( object ( { username : string ( ) . minlength ( 5 ) , email : string ( ) . pattern ( / \S +
Android短信验证码自动填充功能
点击获取验证码按钮,收到短信,当前应用不需要退出程序就可以获取到短信中的验证码,并自动填充,这篇文章主要介绍了Android短信验证码自动填充功能,感兴趣的小伙伴们可以参考一下


