DEV Community

Cover image for HarmonyOS Development: Customize a simple title bar
程序员一鸣
程序员一鸣

Posted on

HarmonyOS Development: Customize a simple title bar

Foreword

this article is based on Api12 

the title bar is standard for almost every application and will exist more or less. A simple combination View in Android can be done. In Hongmeng development, it is also very simple. Select the corresponding layout and then set up the components. 

Image description

You can use relative layout RelativeContainer or linear layout Row, can be implemented, according to the requirements of the project or UI design to put the components, as shown in the figure above, the middle is the title, left and right two components, simple implementation is as follows:

 

Row() {
Text (Return). magic ({left: 10})
Text (left and right text buttons)
Text ("Edit"). marin ({right: 10})
}.width("100%")
.height(50)
.justifyContent(FlexAlign.SpaceBetween)
Enter fullscreen mode Exit fullscreen mode

the above code is only used for the three components on the left, middle and right. It can be found that a title combination component has no difficulty or technical content. In actual development, if there are multiple title bar forms, considering the code reuse, try to extract a custom component, and select the component required by the current page through attribute control. 

Image description

For example, some pages have pictures on the left and pictures on the right, or two buttons on the left, two pictures, or a combination of pictures and words, etc., which need to be considered in actual packaging. 

In view of the above situations, a simple package has also been made, which has been uploaded to the central warehouse and can be directly used by friends in need. 

Central Warehouse Address: 

https://ohpm.openharmony.cn/#/cn/detail/@abner%2Fbar 

quick Use

Method 1: Set the three-party package dependency in the oh-package.json5 that needs the Module. The configuration example is as follows:

 

"dependencies": { "@abner/bar": "^1.0.1"}
Enter fullscreen mode Exit fullscreen mode

method 2: in the Terminal window, run the following command to install the third-party package. DevEco Studio automatically adds the third-party package dependency to the project oh-package.json5. 

Suggestion: Execute the command under the module path used.


ohpm install @abner/bar
Enter fullscreen mode Exit fullscreen mode

Initialization 

the initialization function is used to unify the title bar, such as width and height, font color size, unified click event, etc. It is recommended to be carried out in the AbilityStage, and the attribute is selectively called. If it is not needed, it may not be set.

 

initActionBar({})
Enter fullscreen mode Exit fullscreen mode

Related Properties 

related attributes can be used for global initialization or individually. 

Property type overview
barWidth  Length  title Bar Width 
barHeight  Length  title Bar Height 
barBackgroundColor  ResourceColor  title Bar Background Color 
onTitleClick  callback  header Click Event 
leftText string/Resource  left button text 
left2Text  string/Resource  second left button text 
leftMenuMargin  Length  left button, default distance left 
leftMenuBgColor  ResourceColor  left background color 
rightMenuBgColor  ResourceColor  right background color 
rightMenuMargin  Length  right Button Distance to Right 
leftMenuWidth  Length  overall width of left button 
leftMenuHeight  Length  overall height of left button 
rightMenuWidth  Length  right button width 
rightMenuHeight Length  right button height 
onLeftClick  callback  left overall click 
onLeftTextClick  callback  click on the left text button 
onLeftImageClick  callback  click on the left picture button 
onRightClick  callback  click on the whole right 
onRightTextClick  callback  click on the right text button 
onRightImageClick  callback  click on the picture button on the right 
leftIcon  PixelMap/ ResourceStr/ DrawableDescriptor  left Button First Icon 
left2Icon  PixelMap/ ResourceStr/ DrawableDescriptor  left button second icon 
rightIcon PixelMap/ ResourceStr/ DrawableDescriptor  right button first icon 
right2Icon  PixelMap/ ResourceStr/ DrawableDescriptor  right button second icon 
hideLeftMenu  boolean  hide the left button, not hidden by default 
hideTitle  boolean  hide the title, not hidden by default 
hideRightMenu  boolean  hide the right button, not hidden by default 
isAvoidanceNavigation  boolean  whether to avoid navigation, default not to avoid 
titleAttribute  TitleAttribute  title common properties, color size, etc. 
leftMenuAttribute  BarMenuAttribute  common properties of the first button on the left, color size, etc.
leftMenu2Attribute  BarMenuAttribute  the second button on the left common properties, color size, etc. 
rightMenuAttribute  BarMenuAttribute  common properties of the first button on the right, color size, etc. 
rightMenu2Attribute  BarMenuAttribute  the second button on the right side of the general properties, color size, etc. 

Basic use 

1, simple use, is an ordinary component


 

ActionBar({
Title: "Ordinary Single Title"
})
Enter fullscreen mode Exit fullscreen mode

2. Left text button display


 

ActionBar({
Title: "Left Text Button",
LeftText: "Return",
onLeftClick: () => {
Console.log ("===Left button clicked")
},
})
Enter fullscreen mode Exit fullscreen mode

3. Left picture button


 

ActionBar({
Title: "Left Image Button",
leftIcon: $r("app.media.app_icon"),
onLeftClick: () => {
Console.log ("===Left button clicked")
},
})
Enter fullscreen mode Exit fullscreen mode

4. Right text button


 

ActionBar({
  title: "right text", rightText: "edit",
  onRightClick: () => {
    console.log("====click right")
  }
})
Enter fullscreen mode Exit fullscreen mode

5. The right picture button


 

ActionBar({
  title: "right image",
  rightIcon: $r("app.media.app_icon"),
  onRightClick: () => {
    console.log("====click right")
  }
})
Enter fullscreen mode Exit fullscreen mode

6. Left and right text buttons


 

ActionBar({
  title: "left and right",
  leftText: "back",
  rightText: "edit",
  onLeftClick: () => {
    console.log("====click left")
  },
  onRightClick: () => {
    console.log("====click right")
  }
})
Enter fullscreen mode Exit fullscreen mode

7. Left and right picture buttons


 

ActionBar({
  title: "left and right image",
  rightIcon: $r("app.media.app_icon"),
  leftIcon: $r("app.media.app_icon"),
  onLeftClick: () => {
    console.log("====click left")
  },
  onRightClick: () => {
    console.log("====click right")
  }
})
Enter fullscreen mode Exit fullscreen mode

8. Left text double button


 

ActionBar({
  title: "left text",
  leftText: "button 1",
  left2Text: "button 2",
  leftMenuAttribute: {
    textMargin: { right: 10 }
  },
  onLeftTextClick: (position) => {
    console.log("====click:" + position)
  }
})
Enter fullscreen mode Exit fullscreen mode

9. Left picture double button


 

ActionBar({
  title: "left image",
  leftIcon: $r("app.media.app_icon"),
  left2Icon: $r("app.media.app_icon"),
  leftMenuAttribute: {
    imageMargin: { right: 10 }
  },
  onLeftImageClick: (position) => {
    console.log("====click:" + position)
  }
})
Enter fullscreen mode Exit fullscreen mode

10. Right text double button


 

ActionBar({
  title: "right text",
  rightText: "button 1",
  right2Text: "button 2",
  rightMenuAttribute: {
    textMargin: { left: 10 }
  },
  onRightTextClick: (position) => {
    console.log("====click:" + position)
  }
})
Enter fullscreen mode Exit fullscreen mode

11. The right picture double button


 

ActionBar({
  title: "right text add image",
  rightIcon: $r("app.media.app_icon"),
  right2Icon: $r("app.media.app_icon"),
  rightMenu2Attribute: {
    imageMargin: { left: 10 }
  },
  onRightImageClick: (position) => {
    console.log("====click:" + position)
  }
})
Enter fullscreen mode Exit fullscreen mode

12. Left and right text double buttons


 

ActionBar({
  title: "left and right text",
  leftText: "button 1",
  left2Text: "bnutton 2",
  leftMenuAttribute: {
    textMargin: { right: 10 }
  },
  onLeftTextClick: (position) => {
    console.log("====click:" + position)
  },
  rightText: "button 1",
  right2Text: "button 2",
  rightMenuAttribute: {
    textMargin: { left: 10 }
  },
  onRightTextClick: (position) => {
    console.log("====click:" + position)
  }
})
Enter fullscreen mode Exit fullscreen mode

13. Picture and text on the left


 

ActionBar({
  title: "left image",
  leftText: "back",
  leftIcon: $r("app.media.app_icon"),
  leftMenuType: MenuType.IMAGE_TEXT,
  leftMenuAttribute: {
    imageMargin: {
      right: 10
    }
  },
  onLeftClick: () => {
    console.log("============click")
  }
})
Enter fullscreen mode Exit fullscreen mode

14. Left Text

ActionBar({
  title: "left image",
  leftText: "back",
  leftIcon: $r("app.media.app_icon"),
  leftMenuType: MenuType.TEXT_IMAGE,
  leftMenuAttribute: {
    textMargin: {
      right: 10
    }
  },
  onLeftClick: () => {
    console.log("============click")
  }
})
Enter fullscreen mode Exit fullscreen mode

15. Picture and text on the right

ActionBar({
  title: "right image",
  rightText: "edit",
  rightIcon: $r("app.media.app_icon"),
  rightMenuType: MenuType.IMAGE_TEXT,
  rightMenuAttribute: {
    imageMargin: {
      right: 10
    }
  },
  onRightClick: () => {
    console.log("============click")
  }
})
Enter fullscreen mode Exit fullscreen mode

1*6. Right text*

ActionBar({
  title: "right title",
  rightText: "edit",
  rightIcon: $r("app.media.app_icon"),
  rightMenuType: MenuType.TEXT_IMAGE,
  rightMenuAttribute: {
    textMargin: {
      right: 10
    }
  },
  onRightClick: () => {
    console.log("============click")
  }
})
Enter fullscreen mode Exit fullscreen mode

17. Custom Title

ActionBar({
  titleLayout: this.titleLayout
})
Enter fullscreen mode Exit fullscreen mode

18. The left button defines itself

ActionBar({
  title: "left",
  leftMenuLayout: this.leftMenuLayout
})
Enter fullscreen mode Exit fullscreen mode

19. The right button defines itself

ActionBar({
  title: "right",
  rightMenuLayout: this.rightMenuLayout
})
Enter fullscreen mode Exit fullscreen mode

use summary 

it is a very simple Title bar component, there is no too much technical content, one thing needs to be noted, when using immersion, pay attention to the position of the title bar, need to avoid the status bar.

Top comments (0)