当前位置: 首页 > news >正文

Harmony Next -- 通用标题栏:高度自定义,可设置沉浸式状态,正常状态下为:左侧返回、居中标题,左中右均可自定义视图。

hm_common_title_bar

OpenHarmony三方库中心仓:https://ohpm.openharmony.cn/#/cn/detail/common_title_bar

介绍

一款通用标题栏,支持高度自定义,可设置沉浸式状态,正常状态下为:左侧返回、居中标题,左中右均可自定义视图。

软件架构

Harmony next版本,兼容API12

效果图

在这里插入图片描述

安装教程
  1. ohpm install common_title_bar
使用说明
  1. 引入仓库
  2. 设置全局属性:状态栏高度、是否全面屏、标题栏高度、标题栏颜色等(可忽略,使用默认配置)

基础使用:

import { CommonTitleBar, TitleType } from '@ohos/commonTitleBar'
import { promptAction } from '@kit.ArkUI';@Component
export struct TitlePage {/*** rightType: TitleType.IMAGE时,设置了rightMorePopupData: this.rightMorePopup* 则会显示气泡菜单弹窗:如微信扫一扫弹窗*/@State rightMorePopup: MenuItemOptions[] = [{startIcon: $r('app.media.ic_edit'),content: "编辑资料",}, {startIcon: $r('app.media.ic_download'),content: "上传信息",}, {startIcon: $r('app.media.ic_upload'),content: "下载信息",}, {startIcon: $r('app.media.ic_scan'),content: "扫一扫",}]/*** 左侧自定义视图*/@BuilderleftBuilder() {Row() {Image($r('app.media.ic_arrow_left')).height(20)Text('返回').onClick(() => {promptAction.showToast({message: "点击了左侧自定义视图",duration: 1500,})})}}/*** 右侧自定义视图*/@BuilderrightBuilder() {Row() {Text('更多').onClick(() => {promptAction.showToast({message: "点击了右侧自定义视图",duration: 1500,})})Image($r('app.media.ic_more')).height(20)}}/*** 居中自定义视图*/@BuildercenterBuilder() {Row() {Image($r('app.media.ic_arrow_left')).height(20)Text('居中').onClick(() => {promptAction.showToast({message: "点击了居中自定义视图",duration: 1500,})})Image($r('app.media.ic_more')).height(20)}}build() {NavDestination() {Scroll() {Column() {CommonTitleBar({leftType: TitleType.NONE,centerType: TitleType.TEXT,centerText: "通用标题栏",})CommonTitleBar({isFullScreen: false,leftType: TitleType.TEXT,leftText: "返回",centerType: TitleType.TEXT,centerText: "非全面屏+左右文字",rightType: TitleType.TEXT,rightText: "更多",})/*** 左侧返回,右侧更多+气泡菜单*/CommonTitleBar({isFullScreen: false,centerType: TitleType.TEXT,centerText: "左侧返回+右侧更多",rightType: TitleType.IMAGE,rightMorePopupData: this.rightMorePopup,rightOnClick: (menu, index) => {promptAction.showToast({message: menu?.content + '---' + index,duration: 1500,})}})CommonTitleBar({isFullScreen: false,centerType: TitleType.TEXT,centerText: "自定义点击事件",centerOnClick: (): void => {promptAction.showToast({message: "居中自定义事件",duration: 1500,})},leftOnClick: () => {promptAction.showToast({message: "左侧自定义事件",duration: 1500,})},rightType: TitleType.TEXT,rightText: "更多",rightOnClick: () => {promptAction.showToast({message: "右侧自定义事件",duration: 1500,})}})/*** 自定义视图*/CommonTitleBar({isFullScreen: false,leftType: TitleType.CUSTOM,leftCustomView: this.leftBuilder,centerType: TitleType.CUSTOM,centerCustomView: this.centerBuilder,rightType: TitleType.CUSTOM,rightCustomView: this.rightBuilder})/*** 跑马灯效果*/CommonTitleBar({isFullScreen: false,leftWidth: 80,leftType: TitleType.TEXT,leftText: "超长文本时开启跑马灯效果",centerWidth: 150,centerText: "超长文本时开启跑马灯效果,超长文本时开启跑马灯效果,超长文本时开启跑马灯效果",rightWidth: 80,rightType: TitleType.TEXT,rightText: "超长文本时开启跑马灯效果",rightTextOverflow: TextOverflow.Ellipsis})/*** 中间搜索框*/CommonTitleBar({isFullScreen: false,leftType: TitleType.NONE,centerType: TitleType.SEARCH,searchValue: "关键字",searchPlaceholder: "请输入产品名称",searchButtonText: "搜索",searchButtonOptions: {fontSize: 14,fontColor: Color.Red},onChangeSearch: (value) => {console.log("Search:" + value)},onSubmitSearch: (value) => {promptAction.showToast({message: value,duration: 1500,})}})/*** 中间搜索框 + 左侧返回 + 右侧更多*/CommonTitleBar({isFullScreen: false,rightType: TitleType.IMAGE,rightMorePopupData: this.rightMorePopup,rightOnClick: (menu, index) => {promptAction.showToast({message: menu?.content + '---' + index,duration: 1500,})},centerLeftPadding: 50,centerRightPadding: 50,centerType: TitleType.SEARCH,searchPlaceholder: "请输入产品名称",searchButtonText: "搜索",searchButtonOptions: {fontSize: 14,fontColor: Color.Red},onChangeSearch: (value) => {console.log("Search:" + value)},onSubmitSearch: (value) => {promptAction.showToast({message: value,duration: 1500,})}})/*** 左侧返回方式:* BackType.* POP:使用NavPathStack.pop() 返回到上一页,默认方式* POP_TO_NAME:使用NavPathStack.popToName("name") 返回到上一个name页面* POP_TO_INDEX:使用NavPathStack.popToIndex(1) 返回到索引为1的页面* CLEAR:使用NavPathStack.clear() 返回到根首页(清除栈中所有页面)* ROUTER:使用router.back() 返回到上一页* 取决页面使用Router还是Navigation(推荐)导航*/CommonTitleBar({isFullScreen: false,centerText: "左侧返回方式",leftBackType: BackType.ROUTER})/*** 基础属性设置*/CommonTitleBar({centerText: "基础属性设置",isFullScreen: true,statusBarHeight: 50,statusBarColor: Color.Blue,titleBarHeight: 56,titleBarColor: Color.Green,showBottomLine: true,bottomLineColor: Color.Red,bottomLineSize: 5,})}}}.hideTitleBar(true)}
}

视图类型:

export enum TitleType {NONE = 'none', // 空白TEXT = 'text', // 文字IMAGE = 'image', // 图标CUSTOM = 'custom', // 自定义SEARCH = 'search', // 居中搜索框
}

左侧返回方式:

/*** 返回方式* 使用POP、POP_TO_NAME、POP_TO_INDEX、CLEAR,需要在MainPage主页面设置:@Provide('appPathStack') appPathStack: NavPathStack = new NavPathStack();* MainPage为其他使用CommonTitleBar子组件的主组件* 如果使用以下方式,自定义左侧图图标点击事件(onClickLeftImage)即可*/
export enum BackType {NONE = 'none', // 无点击事件ROUTER = 'router', // router.back() 返回到上一页POP = 'pop', // NavPathStack.pop() 返回到上一页;默认方式POP_TO_NAME = 'popToName', // NavPathStack.popToName("name") 返回到上一个name页面POP_TO_INDEX = 'popToIndex', // NavPathStack.popToIndex(1) 返回到索引为1的页面CLEAR = 'clear', // NavPathStack.clear() 返回到根首页(清除栈中所有页面)
}

CommonTitleBar属性介绍:

属性默认值说明
主体设置
isFullScreenboolean:true是否是全面沉浸是屏
statusBarHeightLength:36状态栏高度(全面沉浸式屏生效)
titleBarHeightLength:56标题栏高度
titleBarColorResourceColor:‘#f5f5f5’标题栏颜色
statusBarColorResourceColor:‘#f5f5f5’状态栏颜色 默认 等于标题栏颜色
showBottomLineboolean:true是否显示标题栏底部的分割线
bottomLineSizeLength:1标题栏底部的分割线的宽度
bottomLineColorResourceColor:“#DDDDDD”标题栏分割线颜色
左侧设置
leftTypestring:TitleType.IMAGE左侧视图类型,默认显示返回按钮
leftWidthLength:-1左侧视图宽度,不设置则自适应内容
leftLeftPaddingLength:15左侧视图左内间距
leftRightPaddingLength:5左侧视图右内间距
leftTextResourceStr:‘Left’左侧文字leftType = text 有效
leftTextColorResourceColor:“#000000”左侧文字颜色
leftTextSizeLength:16左侧文字大小
leftTextOverflowTextOverflow:TextOverflow.MARQUEE左侧文本,超长时的显示方式,默认跑马灯效果;
leftImageResourceResourceStrPixelMap:返回图标
leftImageWidthLength:26左侧图标宽度
leftImageHeightLength:26左侧图标高度
leftImagePaddingLength:5左侧图标padding值:图标尺寸16,内间距各5,保证点击范围
leftBackTypestring:BackType.POP返回方法,不设置leftOnClick的情况下生效
leftPopToNamestring:‘’BackType.POP_TO_NAME下生效:NavPathStack.popToName(“name”) 方式的页面名称
leftPopToIndexnumber:0BackType.POP_TO_INDEX下生效:NavPathStack.popToIndex(1) 返回到索引为1的页面
leftCustomView@Builder:左侧自定义视图
leftOnClick() => void左侧点击事件
右侧设置
rightTypestring:TitleType.NONE右侧视图类型,默认无视图
rightWidthLength:-1右侧宽度,不设置则自适应内容
rightLeftPaddingLength:5右侧视图左内间距
rightRightPaddingLength:15右侧视图右内间距
rightTextResourceStr:‘Right’右侧文字leftType = text 有效
rightTextColorResourceColor:“#000000”右侧文字颜色
rightTextSizeLength:16右侧文字大小
rightTextOverflowTextOverflow:TextOverflow.MARQUEE右侧文本,超长时的显示方式,默认跑马灯效果
rightImageResourceResourceStrPixelMap:更多图标
rightMorePopupView@Builder:更多-气泡菜单如果默认使用更多图标,可默认展示气泡菜单
rightMorePopupDataMenuItemOptions[]:[]气泡菜单数据列表
rightImageWidthLength:26右侧图标宽度
rightImageHeightLength:26右侧图标高度
rightImagePaddingLength:5右侧图标padding值:图标尺寸16,内间距各5,保证点击范围
rightCustomView@Builder:右侧自定义视图
rightOnClick(item?: MenuItemOptions, index?: number) => void右侧点击事件
居中设置
centerTypestring:TitleType.TEXT居中视图类型,默认文字视图
centerWidthLength:-1居中宽度,不设置则自适应内容
centerTextResourceStr:‘Center’居中文字leftType= text 有效
centerTextColorResourceColor:“#000000”居中文字颜色
centerTextSizeLength:16居中文字大小
centerTextOverflowTextOverflow: TextOverflow.MARQUEE居中文本,超长时的显示方式,默认跑马灯效果
centerOnClick() => void居中文字点击事件
centerImageResourceResourceStrPixelMap:无
centerImageWidthLength:26居中图标宽度
centerImageHeightLength:26居中图标高度
centerImagePaddingLength:5居中图标padding值:图标尺寸16,内间距各5,保证点击范围
centerCustomView@Builder:无居中自定义视图
searchValuestring:‘’centerType = TitleType.SEARCH生效:居中搜索框文本
searchPlaceholderResourceStr:‘请输入关键字’居中搜索框提示文本
searchButtonTextstring:‘搜索’设置搜索框末尾搜索按钮文本
searchButtonOptionsSearchButtonOptions:无设置搜索框末尾搜索按钮文本样式
onSubmitSearch(value: string) => void点击搜索图标、搜索按钮或者按下软键盘搜索按钮时触发该回调
onChangeSearch(value: string) => void输入内容发生变化时,触发该回调
centerLeftPaddingLength:30居中视图左内间距
centerRightPaddingLenght:30居中视图右间距

CommonTitleBar全局属性设置:统一设置,整个项目均可生效,一般位于EntryAbility.ets中设置:

/*** 设置通用标题栏的全局属性* 主体设置*/AppStorage.setOrCreate<boolean>(TitleGlobalAttribute.IS_FULL_SCREEN, true); // 全局设置是否是全面屏AppStorage.setOrCreate<number>(TitleGlobalAttribute.STATUS_BAR_HEIGHT, px2vp(area.topRect.height)); // 全局设置状态栏高度AppStorage.setOrCreate<number>(TitleGlobalAttribute.TITLE_BAR_HEIGHT, 56); // 全局设置标题栏高度AppStorage.setOrCreate<ResourceColor>(TitleGlobalAttribute.TITLE_BAR_COLOR, "#f5f5f5"); // 全局设置标题栏颜色AppStorage.setOrCreate<ResourceColor>(TitleGlobalAttribute.STATUS_BAR_COLOR, "#f5f5f5"); // 全局设置状态栏颜色AppStorage.setOrCreate<boolean>(TitleGlobalAttribute.SHOW_BOTTOM_LINE, true); // 全局设置是否显示标题栏底部的分割线AppStorage.setOrCreate<Length>(TitleGlobalAttribute.BOTTOM_LINE_SIZE, 1); // 全局设置标题栏底部的分割线的宽度AppStorage.setOrCreate<ResourceColor>(TitleGlobalAttribute.BOTTOM_LINE_COLOR, "#DDDDDD"); // 全局设置标题栏分割线颜色/*** 左侧设置*/AppStorage.setOrCreate<string>(TitleGlobalAttribute.LEFT_TYPE, TitleType.IMAGE); // 全局设置左侧视图类型AppStorage.setOrCreate<Length>(TitleGlobalAttribute.LEFT_LEFT_PADDING, 15); // 全局设置左侧视图左内间距AppStorage.setOrCreate<Length>(TitleGlobalAttribute.LEFT_RIGHT_PADDING, 5); // 全局设置左侧视图右内间距AppStorage.setOrCreate<ResourceColor>(TitleGlobalAttribute.LEFT_TEXT_COLOR, "#000000"); // 全局设置左侧文字颜色AppStorage.setOrCreate<Length>(TitleGlobalAttribute.LEFT_TEXT_SIZE, 16); // 全局设置左侧文字大小AppStorage.setOrCreate<ResourceStr | PixelMap>(TitleGlobalAttribute.LEFT_IMAGE_RESOURCE,$r('app.media.ic_arrow_left')); // 全局设置左侧图标AppStorage.setOrCreate<Length>(TitleGlobalAttribute.LEFT_IMAGE_WIDTH, 26); // 全局设置左侧图标宽度AppStorage.setOrCreate<Length>(TitleGlobalAttribute.LEFT_IMAGE_HEIGHT, 26); // 全局设置左侧图标高度AppStorage.setOrCreate<Length>(TitleGlobalAttribute.LEFT_IMAGE_PADDING, 5); // 全局设置左侧图标padding/*** 右侧设置*/AppStorage.setOrCreate<string>(TitleGlobalAttribute.RIGHT_TYPE, TitleType.NONE); // 全局设置右侧视图类型AppStorage.setOrCreate<Length>(TitleGlobalAttribute.RIGHT_LEFT_PADDING, 5); // 全局设置右侧视图左内间距AppStorage.setOrCreate<Length>(TitleGlobalAttribute.RIGHT_RIGHT_PADDING, 15); // 全局设置右侧视图右内间距AppStorage.setOrCreate<ResourceColor>(TitleGlobalAttribute.RIGHT_TEXT_COLOR, "#000000"); // 全局设置右侧文字颜色AppStorage.setOrCreate<Length>(TitleGlobalAttribute.RIGHT_TEXT_SIZE, 16); // 全局设置右侧文字大小AppStorage.setOrCreate<ResourceStr | PixelMap>(TitleGlobalAttribute.RIGHT_IMAGE_RESOURCE,$r('app.media.ic_more')); // 全局设置左侧图标AppStorage.setOrCreate<Length>(TitleGlobalAttribute.RIGHT_IMAGE_WIDTH, 26); //  全局设置右侧图标宽度AppStorage.setOrCreate<Length>(TitleGlobalAttribute.RIGHT_IMAGE_HEIGHT, 26); // 全局设置右侧图标高度AppStorage.setOrCreate<Length>(TitleGlobalAttribute.RIGHT_IMAGE_PADDING, 5); // 全局设置右侧图标padding/*** 居中设置*/AppStorage.setOrCreate<string>(TitleGlobalAttribute.CENTER_TYPE, TitleType.TEXT); // 全局设置居中视图类型AppStorage.setOrCreate<Length>(TitleGlobalAttribute.CENTER_LEFT_PADDING, 30); // 全局设置居中视图左内间距AppStorage.setOrCreate<Length>(TitleGlobalAttribute.CENTER_RIGHT_PADDING, 30); // 全局设置居中视图右内间距AppStorage.setOrCreate<ResourceColor>(TitleGlobalAttribute.CENTER_TEXT_COLOR, "#000000"); // 全局设置居中文字颜色AppStorage.setOrCreate<Length>(TitleGlobalAttribute.CENTER_TEXT_SIZE, 16); // 全局设置居中文字大小AppStorage.setOrCreate<ResourceStr | PixelMap>(TitleGlobalAttribute.CENTER_IMAGE_RESOURCE,''); // 全局设置居中图标AppStorage.setOrCreate<Length>(TitleGlobalAttribute.CENTER_IMAGE_WIDTH, 26); //  全局设置居中图标宽度AppStorage.setOrCreate<Length>(TitleGlobalAttribute.CENTER_IMAGE_HEIGHT, 26); // 全局设置居中图标高度AppStorage.setOrCreate<Length>(TitleGlobalAttribute.CENTER_IMAGE_PADDING, 5); // 全局设置居中图标padding
http://www.lryc.cn/news/410499.html

相关文章:

  • 甄选范文“论数据分片技术及其应用”软考高级论文,系统架构设计师论文
  • 【elementui】记录el-table设置左、右列固定时,加大滚动条宽度至使滚动条部分被固定列遮挡的解决方法
  • Python人工智能:一、语音合成和语音识别
  • C/C++进阶 (8)哈希表(STL)
  • 2024电赛H题参考方案(+视频演示+核心控制代码)——自动行驶小车
  • 设计模式14-享元模式
  • Javascript中canvas与svg详解
  • 【BUG】已解决:No Python at ‘C:Users…Python Python39python. exe’
  • Flink SQL 的工作机制
  • [AI Mem0] 源码解读,带你了解 Mem0 的实现
  • 【LLM】-10-部署llama-3-chinese-8b-instruct-v3 大模型
  • C语言 之 理解指针(4)
  • Java设计模式—单例模式(Singleton Pattern)
  • AV1帧间预测(二):运动补偿
  • 数学建模(5)——逻辑回归
  • 【C++高阶】:深入探索C++11
  • 6. 自定义Docker镜像
  • 「12月·长沙」人工智能与网络安全国际学术会议(ISAICS 2024)
  • 【技术支持案例】使用S32K144+NSD8381驱动电子膨胀阀
  • 第二期:集成电路(IC)——智能世界的微观建筑大师
  • 基于物联网的区块链算力网络,IGP/BGP协议
  • 每日一题~960 div2 A+B+C(简单奇偶博弈,构造,观察性质算贡献)
  • 音视频入门基础:H.264专题(17)——FFmpeg源码获取H.264裸流文件信息(视频压缩编码格式、色彩格式、视频分辨率、帧率)的总流程
  • Aboboo一些操作
  • 获取行号LineNumberReader
  • python数据结构与算法
  • 大数据学习之Flink基础(补充)
  • C++基础语法:友元
  • 【大模型系列】Video-LaVIT(2024.06)
  • 【总结】nacos作为注册中心-应用启动失败:NacosDiscoveryProperties{serverAddr=‘127.0.0.1:8848‘……