当前位置:   article > 正文

HarmonyOS自定义标题栏_鸿蒙 设置 窗口 标题

鸿蒙 设置 窗口 标题

1、封装

在pages文件夹下新建文件夹Components 创建Header.ets

  1. @Component
  2. export struct TitleBar {
  3. @Link model: TitleBar.Model;
  4. @BuilderParam customLeftView?: () => any = undefined;
  5. @BuilderParam customCenterView?: () => any = undefined;
  6. @BuilderParam customRightView?: () => any = undefined;
  7. private multipleTitleSwiperController: SwiperController = new SwiperController();
  8. private multipleTitleDataSource: TitleBar.MultipleTitleDataSource = new TitleBar.MultipleTitleDataSource(this.model.multipleTitleList);
  9. /**
  10. * 标题栏两边普通布局
  11. * @param type
  12. */
  13. @Builder normalBothSidesLayout(type: TitleBar.BothSidesLayoutType) {
  14. Row() {
  15. // 图标在上方 文字在下方
  16. if (this.model.leftIconGravity == TitleBar.IconGravity.TOP) {
  17. Column({
  18. space: type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleSpace : this.model.rightTitleSpace
  19. }) {
  20. if ((type == TitleBar.BothSidesLayoutType.LEFT && this.model.leftIcon)
  21. || (type == TitleBar.BothSidesLayoutType.RIGHT && this.model.rightIcon)) {
  22. Image(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIcon : this.model.rightIcon)
  23. .objectFit(ImageFit.Auto)
  24. .width(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconWidth : this.model.rightIconWidth)
  25. .height(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconHeight : this.model.rightIconHeight)
  26. .padding(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconPadding : this.model.rightIconPadding)
  27. }
  28. Text(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleName : this.model.rightTitleName)
  29. .maxLines(1)
  30. .fontSize(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontSize : this.model.rightTitleFontSize)
  31. .fontWeight(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontWeight : this.model.rightTitleFontWeight)
  32. .fontColor(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontColor : this.model.rightTitleFontColor)
  33. }
  34. }
  35. // 图标在下方 文字在上方
  36. else if (this.model.leftIconGravity == TitleBar.IconGravity.BOTTOM) {
  37. Column({
  38. space: type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleSpace : this.model.rightTitleSpace
  39. }) {
  40. Text(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleName : this.model.rightTitleName)
  41. .maxLines(1)
  42. .fontSize(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontSize : this.model.rightTitleFontSize)
  43. .fontWeight(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontWeight : this.model.rightTitleFontWeight)
  44. .fontColor(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontColor : this.model.rightTitleFontColor)
  45. if ((type == TitleBar.BothSidesLayoutType.LEFT && this.model.leftIcon)
  46. || (type == TitleBar.BothSidesLayoutType.RIGHT && this.model.rightIcon)) {
  47. Image(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIcon : this.model.rightIcon)
  48. .objectFit(ImageFit.Auto)
  49. .width(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconWidth : this.model.rightIconWidth)
  50. .height(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconHeight : this.model.rightIconHeight)
  51. .padding(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconPadding : this.model.rightIconPadding)
  52. }
  53. }
  54. }
  55. // 图标在左方 文字在右方
  56. else if (this.model.leftIconGravity == TitleBar.IconGravity.LEFT) {
  57. Row({
  58. space: type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleSpace : this.model.rightTitleSpace
  59. }) {
  60. if ((type == TitleBar.BothSidesLayoutType.LEFT && this.model.leftIcon)
  61. || (type == TitleBar.BothSidesLayoutType.RIGHT && this.model.rightIcon)) {
  62. Image(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIcon : this.model.rightIcon)
  63. .objectFit(ImageFit.Auto)
  64. .width(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconWidth : this.model.rightIconWidth)
  65. .height(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconHeight : this.model.rightIconHeight)
  66. .padding(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconPadding : this.model.rightIconPadding)
  67. }
  68. Text(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleName : this.model.rightTitleName)
  69. .maxLines(1)
  70. .fontSize(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontSize : this.model.rightTitleFontSize)
  71. .fontWeight(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontWeight : this.model.rightTitleFontWeight)
  72. .fontColor(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontColor : this.model.rightTitleFontColor)
  73. }
  74. }
  75. // 图标右方 文字在左方
  76. else if (this.model.leftIconGravity == TitleBar.IconGravity.RIGHT) {
  77. Row({
  78. space: type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleSpace : this.model.rightTitleSpace
  79. }) {
  80. Text(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleName : this.model.rightTitleName)
  81. .maxLines(1)
  82. .fontSize(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontSize : this.model.rightTitleFontSize)
  83. .fontWeight(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontWeight : this.model.rightTitleFontWeight)
  84. .fontColor(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontColor : this.model.rightTitleFontColor)
  85. if ((type == TitleBar.BothSidesLayoutType.LEFT && this.model.leftIcon)
  86. || (type == TitleBar.BothSidesLayoutType.RIGHT && this.model.rightIcon)) {
  87. Image(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIcon : this.model.rightIcon)
  88. .objectFit(ImageFit.Auto)
  89. .width(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconWidth : this.model.rightIconWidth)
  90. .height(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconHeight : this.model.rightIconHeight)
  91. .padding(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconPadding : this.model.rightIconPadding)
  92. }
  93. }
  94. }
  95. }
  96. .justifyContent(FlexAlign.Center)
  97. .alignItems(VerticalAlign.Center)
  98. .constraintSize({
  99. minWidth: this.model.titleBarMinHeight,
  100. minHeight: this.model.titleBarMinHeight,
  101. })
  102. .padding(this.model.titleBarNormalPadding)
  103. .stateStyles({
  104. normal: {
  105. .backgroundColor(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleStateNormalStyleColor : this.model.rightTitleStateNormalStyleColor)
  106. },
  107. pressed: {
  108. .backgroundColor(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleStatePressedStyleColor : this.model.rightTitleStatePressedStyleColor)
  109. }
  110. })
  111. .onClick(() => {
  112. type == TitleBar.BothSidesLayoutType.LEFT ? this.model.onLeftClickListener() : this.model.onRightClickListener();
  113. })
  114. }
  115. /**
  116. * 标题布局
  117. */
  118. @Builder normalCenterLayout() {
  119. Column({ space: this.model.titleSpace }) {
  120. // 主标题
  121. // 跑马灯
  122. if (this.model.titleTextOverflowType == TitleBar.TitleTextOverflowType.MARQUEE) {
  123. Marquee({
  124. start: true,
  125. src: this.model.titleName
  126. })
  127. .fontSize(this.model.titleFontSize)
  128. .fontColor(this.model.titleFontColor)
  129. .fontWeight(this.model.titleFontWeight)
  130. }
  131. // 垂直跑马灯多文本动态显示效果
  132. else if (this.model.titleTextOverflowType == TitleBar.TitleTextOverflowType.MULTIPLE) {
  133. Swiper(this.multipleTitleSwiperController) {
  134. LazyForEach(this.multipleTitleDataSource, (item: string) => {
  135. Text(item)
  136. .maxLines(1)
  137. .textAlign(this.model.multipleTitleTextAlign)
  138. .fontSize(this.model.multipleTitleFontSize)
  139. .fontColor(this.model.multipleTitleFontColor)
  140. .fontWeight(this.model.multipleTitleFontWeight)
  141. .textOverflow({ overflow: this.textOverflowConvert(this.model.multipleTitleTextOverflowType) })
  142. .fontStyle(this.model.multipleTitleTextStyle)
  143. .width('100%')
  144. }, item => item)
  145. }
  146. .autoPlay(true)
  147. .interval(this.model.multipleTitlePlayInterval)
  148. .indicator(false)
  149. .loop(true)
  150. .duration(this.model.multipleTitlePlayDuration)
  151. .itemSpace(this.model.multipleTitlePlayItemSpace)
  152. .vertical(true)
  153. .curve(Curve.Linear)
  154. }
  155. // 普通文本
  156. else {
  157. Text(this.model.titleName)
  158. .maxLines(1)
  159. .fontSize(this.model.titleFontSize)
  160. .fontColor(this.model.titleFontColor)
  161. .fontWeight(this.model.titleFontWeight)
  162. .textOverflow({ overflow: this.textOverflowConvert(this.model.titleTextOverflowType) })
  163. .fontStyle(this.model.titleTextStyle)
  164. }
  165. // 副标题
  166. if (this.model.subTitleName.length != 0) {
  167. if (this.model.subTitleTextOverflowType == TitleBar.TitleTextOverflowType.MARQUEE) {
  168. Marquee({
  169. start: true,
  170. src: this.model.subTitleName
  171. })
  172. .fontSize(this.model.subTitleFontSize)
  173. .fontColor(this.model.subTitleFontColor)
  174. .fontWeight(this.model.subTitleFontWeight)
  175. } else {
  176. Text(this.model.subTitleName)
  177. .maxLines(1)
  178. .fontSize(this.model.subTitleFontSize)
  179. .fontColor(this.model.subTitleFontColor)
  180. .fontWeight(this.model.subTitleFontWeight)
  181. .textOverflow({ overflow: this.textOverflowConvert(this.model.subTitleTextOverflowType) })
  182. .fontStyle(this.model.subTitleTextStyle)
  183. }
  184. }
  185. }
  186. .padding(this.model.titleBarNormalPadding)
  187. .layoutWeight(1)
  188. .onClick(() => {
  189. this.model.onTitleClickListener();
  190. })
  191. }
  192. @Builder blankLayout() {
  193. Row() {
  194. Blank()
  195. }
  196. .constraintSize({
  197. minWidth: this.model.titleBarMinHeight,
  198. minHeight: this.model.titleBarMinHeight,
  199. })
  200. }
  201. textOverflowConvert(type: TitleBar.TitleTextOverflowType): TextOverflow {
  202. if (type == TitleBar.TitleTextOverflowType.NONE) {
  203. return TextOverflow.None;
  204. } else if (type == TitleBar.TitleTextOverflowType.CLIP) {
  205. return TextOverflow.Clip;
  206. } else if (type == TitleBar.TitleTextOverflowType.ELLIPSIS) {
  207. return TextOverflow.Ellipsis;
  208. } else {
  209. return TextOverflow.None;
  210. }
  211. }
  212. aboutToAppear() {
  213. this.model.showLeftSideLayout = !(this.model.leftTitleName.length == 0 && this.model.leftIcon == null);
  214. this.model.showRightSideLayout = !(this.model.rightTitleName.length == 0 && this.model.rightIcon == null);
  215. }
  216. build() {
  217. Row() {
  218. // 左边布局
  219. if (this.customLeftView) {
  220. this.customLeftView()
  221. } else {
  222. if (this.model.showLeftSideLayout) {
  223. this.normalBothSidesLayout(TitleBar.BothSidesLayoutType.LEFT)
  224. } else {
  225. this.blankLayout()
  226. }
  227. }
  228. // 中间布局
  229. if (this.customCenterView) {
  230. this.customCenterView()
  231. } else {
  232. this.normalCenterLayout()
  233. }
  234. // 右边布局
  235. if (this.customRightView) {
  236. this.customRightView()
  237. } else {
  238. if (this.model.showRightSideLayout) {
  239. this.normalBothSidesLayout(TitleBar.BothSidesLayoutType.RIGHT)
  240. } else {
  241. this.blankLayout()
  242. }
  243. }
  244. }
  245. .alignItems(VerticalAlign.Center)
  246. .backgroundColor(this.model.titleBarBackground)
  247. .width('100%')
  248. .constraintSize({
  249. minHeight: this.model.titleBarMinHeight,
  250. })
  251. .border({
  252. width: {
  253. bottom: this.model.titleBarBottomLineHeight
  254. },
  255. color: {
  256. bottom: this.model.titleBarBottomLineColor
  257. }
  258. })
  259. .linearGradient(this.model.titleBarLinearGradient)
  260. }
  261. }
  262. export namespace TitleBar {
  263. /**
  264. * 标题栏样式
  265. */
  266. export enum BarStyle {
  267. // 日间主题样式
  268. LIGHT = 1,
  269. // 夜间主题样式
  270. NIGHT = 2,
  271. // 透明主题样式实现
  272. TRANSPARENT = 3,
  273. // 渐变色样式实现
  274. LINEARGRADIENT = 4,
  275. }
  276. /**
  277. * 标题文本超长时处理显示策略
  278. */
  279. export enum TitleTextOverflowType {
  280. // 文本超长时不进行裁剪。
  281. NONE = 0,
  282. // 文本超长时显示不下的文本用省略号代替。
  283. ELLIPSIS = 1,
  284. // 文本超长时进行裁剪显示。
  285. CLIP = 2,
  286. // 文本超长时跑马灯显示
  287. MARQUEE = 3,
  288. // 垂直跑马灯多文本动态显示效果
  289. MULTIPLE = 4,
  290. }
  291. /**
  292. * 左右两边的操作布局类型 左 右
  293. */
  294. export enum BothSidesLayoutType {
  295. LEFT = 1,
  296. RIGHT = 2,
  297. }
  298. /**
  299. * 左右图标位置 上 下 左 右
  300. */
  301. export enum IconGravity {
  302. TOP = 1,
  303. BOTTOM = 2,
  304. LEFT = 3,
  305. RIGHT = 4,
  306. }
  307. export class Model {
  308. // 标题栏 Padding
  309. titleBarNormalPadding: number | Resource = 5;
  310. // 标题栏最小高度
  311. titleBarMinHeight: number | Resource = 55;
  312. // 标题栏样式
  313. titleBarStyle: BarStyle = BarStyle.LIGHT;
  314. // 标题栏背景色
  315. titleBarBackground: ResourceColor = Color.White;
  316. // 标题栏底部线条颜色
  317. titleBarBottomLineColor: ResourceColor = 0xFFECECEC;
  318. // 标题栏底部线条高度
  319. titleBarBottomLineHeight: number | Resource = 0.35;
  320. // 标题栏渐变色
  321. titleBarLinearGradient: any = null;
  322. /***************************************************************
  323. * 中间标题
  324. ***************************************************************/
  325. // 布局项间距
  326. titleSpace: string | number = 2;
  327. // 标题名称
  328. titleName: string = "";
  329. // 标题颜色
  330. titleFontColor: ResourceColor = Color.Black;
  331. // 标题大小
  332. titleFontSize: number | string | Resource = 18;
  333. // 标题文字粗细
  334. titleFontWeight: number | FontWeight | string = FontWeight.Normal;
  335. // 标题文本超长时处理显示策略
  336. titleTextOverflowType: TitleTextOverflowType = TitleTextOverflowType.ELLIPSIS;
  337. // 字体样式设置 Normal 标准 Italic 斜体
  338. titleTextStyle: FontStyle = FontStyle.Normal;
  339. // 子标题名称
  340. subTitleName: string = "";
  341. // 子标题颜色
  342. subTitleFontColor: ResourceColor = Color.Black;
  343. // 子标题大小
  344. subTitleFontSize: number | string | Resource = 12;
  345. // 子标题文字粗细
  346. subTitleFontWeight: number | FontWeight | string = FontWeight.Normal;
  347. // 子标题文本超长时处理显示策略
  348. subTitleTextOverflowType: TitleTextOverflowType = TitleTextOverflowType.ELLIPSIS;
  349. // 子标题字体样式设置 Normal 标准 Italic 斜体
  350. subTitleTextStyle: FontStyle = FontStyle.Normal;
  351. // 标题栏点击回调
  352. onTitleClickListener: () => void = () => {
  353. };
  354. /***************************************************************
  355. * 垂直跑马灯多文本动态显示效果
  356. ***************************************************************/
  357. multipleTitleList: string[] = [''];
  358. // 垂直跑马灯多文本动态显示标题颜色
  359. multipleTitleFontColor: ResourceColor = Color.Black;
  360. // 垂直跑马灯多文本动态显示标题大小
  361. multipleTitleFontSize: number | string | Resource = 18;
  362. // 垂直跑马灯多文本动态显示标题文字粗细
  363. multipleTitleFontWeight: number | FontWeight | string = FontWeight.Normal;
  364. // 垂直跑马灯多文本动态显示标题文本超长时处理显示策略
  365. multipleTitleTextOverflowType: TitleTextOverflowType = TitleTextOverflowType.ELLIPSIS;
  366. // 垂直跑马灯多文本动态显示字体样式设置 Normal 标准 Italic 斜体
  367. multipleTitleTextStyle: FontStyle = FontStyle.Normal;
  368. // 垂直跑马灯多文本动态显示文字显示位置
  369. multipleTitleTextAlign: TextAlign = TextAlign.Start;
  370. // 垂直跑马灯多文本动态显示文字播放 Interval 间隔
  371. multipleTitlePlayInterval: number = 2000;
  372. // 垂直跑马灯多文本动态显示文字播放 Duration
  373. multipleTitlePlayDuration: number = 2000;
  374. // 垂直跑马灯多文本动态显示文字播放 ItemSpace
  375. multipleTitlePlayItemSpace: number = 5;
  376. /***************************************************************
  377. * 左边按钮
  378. ***************************************************************/
  379. // 左边布局项间距
  380. leftTitleSpace: string | number = 2;
  381. // 左边标题多态样式 Normal
  382. leftTitleStateNormalStyleColor: ResourceColor = Color.White;
  383. // 左边标题多态样式 Pressed
  384. leftTitleStatePressedStyleColor: ResourceColor = Color.Grey;
  385. // 显示左边布局
  386. showLeftSideLayout: boolean = true;
  387. // 左边背景色
  388. leftTitleBackground: ResourceColor = Color.White;
  389. // 左边按钮标题名称
  390. leftTitleName: string = "";
  391. // 左边按钮标题颜色
  392. leftTitleFontColor: ResourceColor = Color.Black;
  393. // 左边按钮标题文字大小
  394. leftTitleFontSize: number | string | Resource = 16;
  395. // 左边按钮标题文字粗细
  396. leftTitleFontWeight: number | FontWeight | string = FontWeight.Normal;
  397. // 左边按钮图标
  398. leftIcon: string | PixelMap | Resource = $r('app.media.left_back_black');
  399. // 左边按钮图标宽度
  400. leftIconWidth: number | string | Resource = 20;
  401. // 左边按钮图标高度
  402. leftIconHeight: number | string | Resource = 20;
  403. // 左边按钮图标 Padding
  404. leftIconPadding: Padding | Length = 0;
  405. // 左边按钮图标位置
  406. leftIconGravity: IconGravity = IconGravity.LEFT;
  407. // 左边点击回调
  408. onLeftClickListener: () => void = () => {
  409. };
  410. /***************************************************************
  411. * 右边按钮
  412. ***************************************************************/
  413. // 右边布局项间距
  414. rightTitleSpace: string | number = 2;
  415. // 右边标题多态样式 Normal
  416. rightTitleStateNormalStyleColor: ResourceColor = Color.White;
  417. // 右边标题多态样式 Pressed
  418. rightTitleStatePressedStyleColor: ResourceColor = Color.Grey;
  419. // 显示右边布局
  420. showRightSideLayout: boolean = true;
  421. // 右边背景色
  422. rightTitleBackground: ResourceColor = Color.White;
  423. // 右边按钮标题名称
  424. rightTitleName: string = "";
  425. // 右边按钮标题颜色
  426. rightTitleFontColor: ResourceColor = Color.Black;
  427. // 右边按钮标题文字大小
  428. rightTitleFontSize: number | string | Resource = 16;
  429. // 右边按钮标题文字粗细
  430. rightTitleFontWeight: number | FontWeight | string = FontWeight.Normal;
  431. // 右边按钮图标
  432. rightIcon: string | PixelMap | Resource = null;
  433. // 右边按钮图标宽度
  434. rightIconWidth: number | string | Resource = 20;
  435. // 右边按钮图标高度
  436. rightIconHeight: number | string | Resource = 20;
  437. // 右边按钮图标 Padding
  438. rightIconPadding: Padding | Length = 0;
  439. // 右边按钮图标位置
  440. rightIconGravity: IconGravity = IconGravity.LEFT;
  441. // 右边点击回调
  442. onRightClickListener: () => void = () => {
  443. };
  444. constructor() {
  445. this.barStyleTransform(BarStyle.LIGHT);
  446. }
  447. setTitleBarNormalPadding(value: number | Resource): Model {
  448. this.titleBarNormalPadding = value;
  449. return this;
  450. }
  451. setTitleBarMinHeight(value: number | Resource): Model {
  452. this.titleBarMinHeight = value;
  453. return this;
  454. }
  455. setTitleBarStyle(value: BarStyle): Model {
  456. this.titleBarStyle = value;
  457. this.barStyleTransform(value);
  458. return this;
  459. }
  460. setTitleBarBackground(value: ResourceColor): Model {
  461. this.titleBarBackground = value;
  462. return this;
  463. }
  464. setTitleBarBottomLineHeight(value: number | Resource): Model {
  465. this.titleBarBottomLineHeight = value;
  466. return this;
  467. }
  468. setTitleBarBottomLineColor(value: ResourceColor): Model {
  469. this.titleBarBottomLineColor = value;
  470. return this;
  471. }
  472. setShowTitleBarBottomLine(value: boolean): Model {
  473. this.titleBarBottomLineHeight = value ? 0.35 : 0;
  474. return this;
  475. }
  476. setTitleBarLinearGradient(value: any): Model {
  477. this.titleBarLinearGradient = value;
  478. return this;
  479. }
  480. /***************************************************************
  481. * 中间标题
  482. ***************************************************************/
  483. setTitleSpace(value: string | number): Model {
  484. this.titleSpace = value;
  485. return this;
  486. }
  487. setTitleName(value: string): Model {
  488. this.titleName = value;
  489. return this;
  490. }
  491. setTitleFontColor(value: ResourceColor): Model {
  492. this.titleFontColor = value;
  493. return this;
  494. }
  495. setTitleFontSize(value: number | string | Resource): Model {
  496. this.titleFontSize = value;
  497. return this;
  498. }
  499. setTitleFontWeight(value: number | FontWeight | string): Model {
  500. this.titleFontWeight = value;
  501. return this;
  502. }
  503. setTitleTextOverflowType(value: TitleTextOverflowType): Model {
  504. this.titleTextOverflowType = value;
  505. return this;
  506. }
  507. setTitleTextStyle(value: FontStyle): Model {
  508. this.titleTextStyle = value;
  509. return this;
  510. }
  511. setSubTitleName(value: string): Model {
  512. this.subTitleName = value;
  513. return this;
  514. }
  515. setSubTitleFontColor(value: ResourceColor): Model {
  516. this.subTitleFontColor = value;
  517. return this;
  518. }
  519. setSubTitleFontSize(value: number | string | Resource): Model {
  520. this.subTitleFontSize = value;
  521. return this;
  522. }
  523. setSubTitleFontWeight(value: number | FontWeight | string): Model {
  524. this.subTitleFontWeight = value;
  525. return this;
  526. }
  527. setSubTitleTextOverflowType(value: TitleTextOverflowType): Model {
  528. this.subTitleTextOverflowType = value;
  529. return this;
  530. }
  531. setSubTitleTextStyle(value: FontStyle): Model {
  532. this.subTitleTextStyle = value;
  533. return this;
  534. }
  535. setOnTitleClickListener(callback: () => void): Model {
  536. this.onTitleClickListener = callback
  537. return this
  538. }
  539. /***************************************************************
  540. * 垂直跑马灯多文本动态显示效果
  541. ***************************************************************/
  542. setMultipleTitleList(value: string[]): Model {
  543. this.multipleTitleList = value;
  544. return this;
  545. }
  546. setMultipleTitleFontColor(value: ResourceColor): Model {
  547. this.multipleTitleFontColor = value;
  548. return this;
  549. }
  550. setMultipleTitleFontSize(value: number | string | Resource): Model {
  551. this.multipleTitleFontSize = value;
  552. return this;
  553. }
  554. setMultipleTitleFontWeight(value: number | FontWeight | string): Model {
  555. this.multipleTitleFontWeight = value;
  556. return this;
  557. }
  558. setMultipleTitleTextOverflowType(value: TitleTextOverflowType): Model {
  559. this.multipleTitleTextOverflowType = value;
  560. return this;
  561. }
  562. setMultipleTitleTextStyle(value: FontStyle): Model {
  563. this.multipleTitleTextStyle = value;
  564. return this;
  565. }
  566. setMultipleTitleTextAlign(value: TextAlign): Model {
  567. this.multipleTitleTextAlign = value;
  568. return this;
  569. }
  570. setMultipleTitlePlayInterval(value: number): Model {
  571. this.multipleTitlePlayInterval = value;
  572. return this;
  573. }
  574. setMultipleTitlePlayDuration(value: number): Model {
  575. this.multipleTitlePlayDuration = value;
  576. return this;
  577. }
  578. setMultipleTitlePlayItemSpace(value: number): Model {
  579. this.multipleTitlePlayItemSpace = value;
  580. return this;
  581. }
  582. /***************************************************************
  583. * 左边按钮
  584. ***************************************************************/
  585. setLeftTitleStateNormalStyleColor(value: ResourceColor): Model {
  586. this.leftTitleStateNormalStyleColor = value;
  587. return this;
  588. }
  589. setLeftTitleStatePressedStyleColor(value: ResourceColor): Model {
  590. this.leftTitleStatePressedStyleColor = value;
  591. return this;
  592. }
  593. setLeftTitleSpace(value: string | number): Model {
  594. this.leftTitleSpace = value;
  595. return this;
  596. }
  597. setShowLeftSideLayout(value: boolean): Model {
  598. this.showLeftSideLayout = value;
  599. return this;
  600. }
  601. setLeftTitleName(value: string): Model {
  602. this.leftTitleName = value;
  603. return this;
  604. }
  605. setLeftTitleFontColor(value: ResourceColor): Model {
  606. this.leftTitleFontColor = value;
  607. return this;
  608. }
  609. setLeftTitleFontSize(value: number | string | Resource): Model {
  610. this.leftTitleFontSize = value;
  611. return this;
  612. }
  613. setLeftTitleFontWeight(value: number | FontWeight | string): Model {
  614. this.leftTitleFontWeight = value;
  615. return this;
  616. }
  617. setLeftIcon(value: string | PixelMap | Resource): Model {
  618. this.leftIcon = value;
  619. return this;
  620. }
  621. setLeftIconWidth(value: number | string | Resource): Model {
  622. this.leftIconWidth = value;
  623. return this;
  624. }
  625. setLeftIconHeight(value: number | string | Resource): Model {
  626. this.leftIconHeight = value;
  627. return this;
  628. }
  629. setLeftIconPadding(value: Padding | Length): Model {
  630. this.leftIconPadding = value;
  631. return this;
  632. }
  633. setLeftIconGravity(value: IconGravity): Model {
  634. this.leftIconGravity = value;
  635. return this;
  636. }
  637. setOnLeftClickListener(callback: () => void): Model {
  638. this.onLeftClickListener = callback
  639. return this
  640. }
  641. setLeftTitleBackground(value: ResourceColor): Model {
  642. this.leftTitleBackground = value;
  643. return this;
  644. }
  645. /***************************************************************
  646. * 右边按钮
  647. ***************************************************************/
  648. setRightTitleStateNormalStyleColor(value: ResourceColor): Model {
  649. this.rightTitleStateNormalStyleColor = value;
  650. return this;
  651. }
  652. setRightTitleStatePressedStyleColor(value: ResourceColor): Model {
  653. this.rightTitleStatePressedStyleColor = value;
  654. return this;
  655. }
  656. setRightTitleSpace(value: string | number): Model {
  657. this.rightTitleSpace = value;
  658. return this;
  659. }
  660. setShowRightSideLayout(value: boolean): Model {
  661. this.showRightSideLayout = value;
  662. return this;
  663. }
  664. setRightTitleName(value: string): Model {
  665. this.rightTitleName = value;
  666. return this;
  667. }
  668. setRightTitleFontColor(value: ResourceColor): Model {
  669. this.rightTitleFontColor = value;
  670. return this;
  671. }
  672. setRightTitleFontSize(value: number | string | Resource): Model {
  673. this.rightTitleFontSize = value;
  674. return this;
  675. }
  676. setRightTitleFontWeight(value: number | FontWeight | string): Model {
  677. this.rightTitleFontWeight = value;
  678. return this;
  679. }
  680. setRightIcon(value: string | PixelMap | Resource): Model {
  681. this.rightIcon = value;
  682. return this;
  683. }
  684. setRightIconWidth(value: number | string | Resource): Model {
  685. this.rightIconWidth = value;
  686. return this;
  687. }
  688. setRightIconHeight(value: number | string | Resource): Model {
  689. this.rightIconHeight = value;
  690. return this;
  691. }
  692. setRightIconPadding(value: Padding | Length): Model {
  693. this.rightIconPadding = value;
  694. return this;
  695. }
  696. setRightIconGravity(value: IconGravity): Model {
  697. this.rightIconGravity = value;
  698. return this;
  699. }
  700. setOnRightClickListener(callback: () => void): Model {
  701. this.onRightClickListener = callback
  702. return this
  703. }
  704. setRightTitleBackground(value: ResourceColor): Model {
  705. this.rightTitleBackground = value;
  706. return this;
  707. }
  708. /**
  709. * 标题样式转换
  710. * @param value
  711. */
  712. private barStyleTransform(value: BarStyle) {
  713. switch (value) {
  714. case TitleBar.BarStyle.LIGHT:
  715. this.setTitleBarBackground(0xFFFFFFFF);
  716. this.setTitleFontColor(0xFF222222);
  717. this.setLeftTitleStateNormalStyleColor(0xFFFFFFFF);
  718. this.setLeftTitleStatePressedStyleColor(0x0C000000);
  719. this.setLeftIcon($r('app.media.left_back_black'));
  720. this.setLeftTitleFontColor(0xFF666666);
  721. this.setRightTitleStateNormalStyleColor(0xFFFFFFFF);
  722. this.setRightTitleStatePressedStyleColor(0x0C000000);
  723. this.setRightTitleFontColor(0xFFA4A4A4);
  724. break;
  725. case TitleBar.BarStyle.NIGHT:
  726. this.setTitleBarBackground(0xFF000000);
  727. this.setTitleFontColor(0xEEFFFFFF);
  728. this.setLeftTitleStateNormalStyleColor(0x00000000);
  729. this.setLeftTitleStatePressedStyleColor(0x66FFFFFF);
  730. this.setLeftIcon($r('app.media.left_back_white'));
  731. this.setLeftTitleFontColor(0xCCFFFFFF);
  732. this.setRightTitleStateNormalStyleColor(0x00000000);
  733. this.setRightTitleStatePressedStyleColor(0x66FFFFFF);
  734. this.setRightTitleFontColor(0xCCFFFFFF);
  735. break;
  736. case TitleBar.BarStyle.TRANSPARENT:
  737. this.setTitleBarBackground(Color.Transparent);
  738. this.setTitleFontColor(0xFFFFFFFF);
  739. this.setLeftTitleStateNormalStyleColor(Color.Transparent);
  740. this.setLeftTitleStatePressedStyleColor(0x22000000);
  741. this.setLeftIcon(null);
  742. this.setLeftTitleFontColor(0xFFFFFFFF);
  743. this.setRightTitleStateNormalStyleColor(Color.Transparent);
  744. this.setRightTitleStatePressedStyleColor(0x22000000);
  745. this.setRightTitleFontColor(0xFFFFFFFF);
  746. break;
  747. case TitleBar.BarStyle.LINEARGRADIENT:
  748. this.setTitleBarBackground(Color.Transparent);
  749. this.setTitleFontColor(0xFFFFFFFF);
  750. this.setLeftTitleStateNormalStyleColor(Color.Transparent);
  751. this.setLeftTitleStatePressedStyleColor(0x22000000);
  752. this.setLeftIcon(null);
  753. this.setLeftTitleFontColor(0xFFFFFFFF);
  754. this.setRightTitleStateNormalStyleColor(Color.Transparent);
  755. this.setRightTitleStatePressedStyleColor(0x22000000);
  756. this.setRightTitleFontColor(0xFFFFFFFF);
  757. break;
  758. default:
  759. break;
  760. }
  761. }
  762. }
  763. export class MultipleTitleDataSource implements IDataSource {
  764. private multipleTitleList: string[] = [];
  765. private listener: DataChangeListener;
  766. constructor(list: string[]) {
  767. this.multipleTitleList = list;
  768. }
  769. totalCount(): number {
  770. return this.multipleTitleList.length;
  771. }
  772. getData(index: number): any {
  773. return this.multipleTitleList[index];
  774. }
  775. registerDataChangeListener(listener: DataChangeListener): void {
  776. this.listener = listener;
  777. }
  778. unregisterDataChangeListener() {
  779. this.listener = null;
  780. }
  781. }
  782. }
  783. export default TitleBar

2、使用

  1. import { TitleBar } from '../Components/Header'
  2. @State model1: TitleBar.Model = new TitleBar.Model()
  3. .setOnLeftClickListener(() => {
  4. router.back()
  5. })
  6. .setLeftTitleName("返回")
  7. .setTitleName("咨询订单")
  8. build() {
  9. TitleBar({ model: $model1 })
  10. }

3、效果

部分内容转载:https://www.wpsshop.cn/w/你好赵伟/article/detail/228594

推荐阅读
相关标签
  

闽ICP备14008679号