• 华为鸿蒙应用--封装通用标题栏:CommonTitleBar(鸿蒙工具)-ArkTs


    0、效果图

    自定义通用标题栏

    支持左、中、右常规标题栏设置;

    支持自定义视图

    支持搜索功能

    一、CommTitleBar代码
    1. import router from '@ohos.router';
    2. import { Constants } from '../../constants/Constants';
    3. import { StyleConstants } from '../../constants/StyleConstants'
    4. import { toast } from '../../utils/ToastUtils';
    5. @Component
    6. export struct CommonTitleBar {
    7. @State titleBarHeight: number = 50; // 标题栏高度
    8. @State showBottomLine: boolean = true; // 是否显示标题栏底部的分割线
    9. titleBarColor: ResourceColor = '#f5f5f5'; // 标题栏颜色
    10. bottomLineColor: ResourceColor = "#DDDDDD"; // 标题栏分割线颜色
    11. @State leftType: number = Constants.TYPE_LEFT_IMAGE_VIEW; // 左侧视图类型:无|文字|按钮|自定义视图,默认显示返回按钮
    12. @State leftText: string = '左侧文字'; // 左侧文字leftType= textView有效
    13. @State leftMargin: number | string = 12; // 左侧间距
    14. leftTextColor: ResourceColor = "#000000"; // 左侧文字颜色
    15. @State leftTextSize: number | string = 16; // 左侧文字大小
    16. leftImageResource: string | PixelMap | Resource = $r('app.media.img_back_black'); // 左侧返回图片
    17. @BuilderParam leftCustomView: () => void; // 左侧自定义View
    18. onClickLeftText?: () => void; // 左侧文字点击事件
    19. onClickLeftImage?: () => void; // 左侧图片点击事件
    20. @State rightType: number = Constants.TYPE_RIGHT_NONE; // 右侧视图类型:无|文字|按钮|自定义视图,默认无视图
    21. @State rightText: string = '右侧文字'; // 右侧文字leftType= textView有效
    22. @State rightMargin: number | string = 12; // 右侧间距
    23. rightTextColor: ResourceColor = "#000000"; // 右侧文字颜色
    24. @State rightTextSize: number | string = 16; // 右侧文字大小
    25. rightImageResource: string | PixelMap | Resource = $r('app.media.img_back_black'); // 右侧图片
    26. @BuilderParam rightCustomView: () => void; // 右侧自定义View
    27. onClickRightText?: () => void; // 右侧文字点击事件
    28. onClickRightImage?: () => void; // 右侧图片点击事件
    29. @State centerType: number = Constants.TYPE_CENTER_TEXT_VIEW; // 居中视图类型:无|文字|按钮|自定义视图,默认文字视图
    30. @State centerText: string = '居中文字'; // 居中文字leftType= textView有效
    31. centerTextColor: ResourceColor = "#000000"; // 居中文字颜色
    32. @State centerTextSize: number | string = 16; // 居中文字大小
    33. centerImageResource: string | PixelMap | Resource = $r('app.media.img_back_black'); // 居中图片
    34. @State searchLeftMargin: number | string = 12; // 右侧间距
    35. @State searchRightMargin: number | string = 12; // 右侧间距
    36. @BuilderParam centerCustomView: () => void; // 居中自定义View
    37. onClickCenterText?: () => void; // 居中文字点击事件
    38. onClickCenterImage?: () => void; // 居中图片点击事件
    39. @State value: string = ''; // 居中搜索框默认文本
    40. @State placeholder: string = '请输入关键字'; // 居中搜索框提示文本
    41. @State searchButton: string = ''; // 居中搜索框提示文本
    42. onSubmitSearch?: (value: string) => void; // 点击搜索图标、搜索按钮或者按下软键盘搜索按钮时触发该回调
    43. onChangeSearch?: (value: string) => void; // 输入内容发生变化时,触发该回调
    44. build() {
    45. RelativeContainer() {
    46. if (this.leftType === Constants.TYPE_LEFT_TEXT_VIEW) {
    47. Text(this.leftText)
    48. .alignRules({
    49. left: { anchor: "__container__", align: HorizontalAlign.Start },
    50. center: { anchor: "__container__", align: VerticalAlign.Center }
    51. })
    52. .margin({
    53. left: this.leftMargin
    54. })
    55. .fontColor(this.leftTextColor)
    56. .fontSize(this.leftTextSize)
    57. .maxLines(1)
    58. .onClick(() => {
    59. if (this.onClickLeftText !== undefined) {
    60. this.onClickLeftText();
    61. }
    62. })
    63. .id('left')
    64. }
    65. else if (this.leftType === Constants.TYPE_LEFT_IMAGE_VIEW) {
    66. Image(this.leftImageResource)
    67. .height(18)
    68. .alignRules({
    69. left: { anchor: "__container__", align: HorizontalAlign.Start },
    70. center: { anchor: "__container__", align: VerticalAlign.Center }
    71. })
    72. .margin({
    73. left: this.leftMargin
    74. })
    75. .onClick(() => {
    76. if (this.onClickLeftImage !== undefined) {
    77. this.onClickLeftImage();
    78. } else {
    79. router.back()
    80. }
    81. })
    82. .id('left')
    83. }
    84. else if (this.leftType === Constants.TYPE_LEFT_CUSTOM_VIEW) {
    85. Column() {
    86. this.leftCustomView()
    87. }.alignRules({
    88. left: { anchor: "__container__", align: HorizontalAlign.Start },
    89. center: { anchor: "__container__", align: VerticalAlign.Center }
    90. })
    91. .margin({
    92. left: this.leftMargin
    93. })
    94. .id('left')
    95. }
    96. if (this.rightType === Constants.TYPE_RIGHT_TEXT_VIEW) {
    97. Text(this.rightText)
    98. .alignRules({
    99. right: { anchor: "__container__", align: HorizontalAlign.End },
    100. center: { anchor: "__container__", align: VerticalAlign.Center }
    101. })
    102. .margin({
    103. right: this.rightMargin
    104. })
    105. .fontColor(this.rightTextColor)
    106. .fontSize(this.rightTextSize)
    107. .maxLines(1)
    108. .onClick(() => {
    109. if (this.onClickRightText !== undefined) {
    110. this.onClickRightText();
    111. }
    112. })
    113. .id('right')
    114. }
    115. else if (this.rightType === Constants.TYPE_RIGHT_IMAGE_VIEW) {
    116. Image(this.rightImageResource)
    117. .height(18)
    118. .alignRules({
    119. right: { anchor: "__container__", align: HorizontalAlign.End },
    120. center: { anchor: "__container__", align: VerticalAlign.Center }
    121. })
    122. .margin({
    123. right: this.rightMargin
    124. })
    125. .onClick(() => {
    126. if (this.onClickRightImage !== undefined) {
    127. this.onClickRightImage();
    128. } else {
    129. toast("点击了")
    130. }
    131. })
    132. .id('right')
    133. }
    134. else if (this.rightType === Constants.TYPE_RIGHT_CUSTOM_VIEW) {
    135. Column() {
    136. this.rightCustomView()
    137. }.alignRules({
    138. right: { anchor: "__container__", align: HorizontalAlign.End },
    139. center: { anchor: "__container__", align: VerticalAlign.Center }
    140. })
    141. .margin({
    142. right: this.rightMargin
    143. })
    144. .id('right')
    145. }
    146. if (this.centerType === Constants.TYPE_CENTER_TEXT_VIEW) {
    147. Text(this.centerText)
    148. .alignRules({
    149. middle: { anchor: "__container__", align: HorizontalAlign.Center },
    150. center: { anchor: "__container__", align: VerticalAlign.Center }
    151. })
    152. .fontColor(this.centerTextColor)
    153. .fontSize(this.centerTextSize)
    154. .maxLines(1)
    155. .onClick(() => {
    156. if (this.onClickCenterText !== undefined) {
    157. this.onClickCenterText();
    158. }
    159. })
    160. .id('center_text')
    161. }
    162. else if (this.centerType === Constants.TYPE_CENTER_IMAGE_VIEW) {
    163. Image(this.centerImageResource)
    164. .height(18)
    165. .alignRules({
    166. middle: { anchor: "__container__", align: HorizontalAlign.Center },
    167. center: { anchor: "__container__", align: VerticalAlign.Center }
    168. })
    169. .onClick(() => {
    170. if (this.onClickRightImage !== undefined) {
    171. this.onClickRightImage();
    172. } else {
    173. toast("点击了")
    174. }
    175. })
    176. .id('center_image')
    177. }
    178. else if (this.centerType === Constants.TYPE_CENTER_CUSTOM_VIEW) {
    179. Column() {
    180. this.centerCustomView()
    181. }.alignRules({
    182. middle: { anchor: "__container__", align: HorizontalAlign.Center },
    183. center: { anchor: "__container__", align: VerticalAlign.Center }
    184. })
    185. .id('center_custom')
    186. }
    187. else if (this.centerType === Constants.TYPE_CENTER_SEARCH_VIEW) {
    188. Column() {
    189. Search({
    190. value: this.value,
    191. placeholder: this.placeholder,
    192. })
    193. .height(38)
    194. .searchButton(this.searchButton)
    195. .onSubmit((value: string) => {
    196. this.onSubmitSearch(value);
    197. })
    198. .onChange((value: string) => {
    199. this.onChangeSearch(value);
    200. })
    201. }
    202. .alignRules({
    203. left: { anchor: "left", align: HorizontalAlign.End },
    204. right: { anchor: "right", align: HorizontalAlign.Start },
    205. center: { anchor: "__container__", align: VerticalAlign.Center },
    206. })
    207. .margin({
    208. left: this.searchLeftMargin,
    209. right: this.searchRightMargin
    210. })
    211. .id('center_search')
    212. }
    213. }
    214. .width(StyleConstants.FULL_WIDTH)
    215. .height(this.titleBarHeight)
    216. .backgroundColor(this.titleBarColor)
    217. .border({
    218. width: { bottom: this.showBottomLine ? '1vp' : '0' },
    219. })
    220. .borderColor(this.bottomLineColor)
    221. }
    222. }
    Constants:
    1. // 自定义标题栏
    2. static readonly TYPE_LEFT_NONE: number = 0;
    3. static readonly TYPE_LEFT_TEXT_VIEW: number = 1;
    4. static readonly TYPE_LEFT_IMAGE_VIEW: number = 2;
    5. static readonly TYPE_LEFT_CUSTOM_VIEW: number = 3;
    6. static readonly TYPE_RIGHT_NONE: number = 0;
    7. static readonly TYPE_RIGHT_TEXT_VIEW: number = 1;
    8. static readonly TYPE_RIGHT_IMAGE_VIEW: number = 2;
    9. static readonly TYPE_RIGHT_CUSTOM_VIEW: number = 3;
    10. static readonly TYPE_CENTER_NONE: number = 0;
    11. static readonly TYPE_CENTER_TEXT_VIEW: number = 1;
    12. static readonly TYPE_CENTER_IMAGE_VIEW: number = 2;
    13. static readonly TYPE_CENTER_CUSTOM_VIEW: number = 3;
    14. static readonly TYPE_CENTER_SEARCH_VIEW: number = 4;
    二、调用方法
    1. import { CommonTitleBar, Constants, StyleConstants, toast } from '@ohos/common'
    2. @Component
    3. export struct Contact {
    4. @Builder leftBuilder() {
    5. Row() {
    6. Text('自定义')
    7. .onClick(() => {
    8. toast('点击自定义')
    9. })
    10. Image($r('app.media.img_select'))
    11. .height(20)
    12. }
    13. }
    14. @Builder rightBuilder() {
    15. Row() {
    16. Text('自定义')
    17. .onClick(() => {
    18. toast('点击自定义')
    19. })
    20. Image($r('app.media.img_select'))
    21. .height(20)
    22. }
    23. }
    24. @Builder centerBuilder() {
    25. Row() {
    26. Text('自定义')
    27. .onClick(() => {
    28. toast('点击自定义')
    29. })
    30. Image($r('app.media.img_select'))
    31. .height(20)
    32. }
    33. }
    34. build() {
    35. Column() {
    36. CommonTitleBar({
    37. leftType: Constants.TYPE_LEFT_CUSTOM_VIEW,
    38. leftCustomView: this.leftBuilder,
    39. leftText: "企业通讯录",
    40. rightType: Constants.TYPE_RIGHT_CUSTOM_VIEW,
    41. rightCustomView: this.rightBuilder,
    42. rightText: '确定',
    43. centerType: Constants.TYPE_CENTER_SEARCH_VIEW,
    44. centerText: "居中",
    45. centerCustomView: this.centerBuilder,
    46. onClickLeftText: (): void => {
    47. toast('AAA')
    48. },
    49. onClickLeftImage: (): void => {
    50. toast('onClickLeftImage')
    51. },
    52. onSubmitSearch: (value): void => {
    53. toast(value);
    54. },
    55. onChangeSearch: (value): void => {
    56. toast(value);
    57. }
    58. })
    59. }
    60. .width(StyleConstants.FULL_WIDTH)
    61. .height(StyleConstants.FULL_WIDTH)
    62. }
    63. }

  • 相关阅读:
    MySQL索引失效场景以及解决方案
    线上AB实验的日志分析
    swift语言用哪种库适合做爬虫?
    springboot整合shiro问题
    关于go协程超时退出控制条件与方式分析
    dart 学习 之 异常
    入门力扣自学笔记119 C++ (题目编号640)
    2023年云南省职业院校技能大赛中职组 “网络安全”赛项竞赛任务书
    时间空间复杂度分析--选择排序算法
    链路负载均衡之全局选路策略
  • 原文地址:https://blog.csdn.net/qq_41374940/article/details/138169425