之前已经写过一个在HarmonyOS中的自定义组件的案例,里面主要讲解了DrawTask这个接口的使用,从而让我们可以调用Canvas进行绘制。

在之前的案例帖子中,有人回复问我如何实现自定义属性,现在这篇专门针对自定义属性写一篇帖子,同时通过自定义属性自己封装了一个非常实用的标题栏TitleBar

不多说,首先上效果图

通过鸿蒙自定义属性,来创造一个可以为所欲为的自定义标题组件

这里主要真多标题栏的背景,标题文字、大小、颜色,左右两侧按钮是图标显示还是文字显示、是否显示分别进行了定制,后期用户使用只需要通过几个简单自定义属性的配置即可组合实现自己想要的效果。

具体实现思路如下,首先创建一个HarmonyOS Library模块mycustomtitlebar,在里面添加一个布局layout_titlebar.xml,代码如下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DependentLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_content"
  5. ohos:width="match_parent">
  6. <Button
  7. ohos:id="$+id:title_bar_left"
  8. ohos:height="match_content"
  9. ohos:width="match_content"
  10. ohos:align_parent_start="true"
  11. ohos:left_padding="5vp"
  12. ohos:min_height="45vp"
  13. ohos:min_width="45vp"
  14. ohos:text_size="14fp"
  15. ohos:vertical_center="true"/>
  16. <Text
  17. ohos:id="$+id:titleText"
  18. ohos:height="match_content"
  19. ohos:width="match_content"
  20. ohos:center_in_parent="true"
  21. ohos:multiple_lines="false"
  22. ohos:text_size="17fp"/>
  23. <Button
  24. ohos:id="$+id:title_bar_right"
  25. ohos:height="match_content"
  26. ohos:width="match_content"
  27. ohos:align_parent_end="true"
  28. ohos:left_padding="5vp"
  29. ohos:min_height="45vp"
  30. ohos:min_width="45vp"
  31. ohos:right_margin="5vp"
  32. ohos:text_size="14fp"
  33. ohos:vertical_center="true"/>
  34. </DependentLayout>

然后创建一个自定义组件对应的类CustomTitleBar,代码如下:

  1. package com.xdw.mycustomtitlebar;
  2. import ohos.agp.components.*;
  3. import ohos.agp.utils.Color;
  4. import ohos.app.Context;
  5. import ohos.hiviewdfx.HiLog;
  6. import ohos.hiviewdfx.HiLogLabel;
  7. /**
  8. * Created by 夏德旺 on 2021/3/4 10:01
  9. */
  10. public class CustomTitleBar extends ComponentContainer {
  11. private static final String TAG = "CustomTitleBar";
  12. private static final HiLogLabel LABEL = new HiLogLabel(HiLog.DEBUG, 0, "TAG");
  13. public CustomTitleBar(Context context) {
  14. super(context);
  15. }
  16. public CustomTitleBar(Context context, AttrSet attrSet) {
  17. super(context, attrSet);
  18. //动态加载layout
  19. Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_titlebar, null, false);
  20. Button leftBtn = (Button) component.findComponentById(ResourceTable.Id_title_bar_left);
  21. Text titleText = (Text) component.findComponentById(ResourceTable.Id_titleText);
  22. Button rightBtn = (Button) component.findComponentById(ResourceTable.Id_title_bar_right);
  23. //添加layout到父组件
  24. addComponent(component);
  25. //处理TitleBar背景色
  26. if(attrSet.getAttr("bg_color").isPresent()){
  27. component.setBackground(attrSet.getAttr("bg_color").get().getElement());
  28. }else{
  29. HiLog.error(LABEL,"attr bg_color is not present");
  30. component.setBackground(getBackgroundElement());
  31. }
  32. //处理标题文字
  33. if(attrSet.getAttr("title_text").isPresent()){
  34. titleText.setText(attrSet.getAttr("title_text").get().getStringValue());
  35. }else {
  36. HiLog.error(LABEL,"attr title_text is not present");
  37. titleText.setText("");
  38. }
  39. //处理标题大小
  40. if(attrSet.getAttr("title_size").isPresent()){
  41. titleText.setTextSize(attrSet.getAttr("title_size").get().getIntegerValue(), Text.TextSizeType.FP);
  42. }else {
  43. HiLog.error(LABEL,"attr title_size is not present");
  44. }
  45. //处理标题颜色
  46. if(attrSet.getAttr("title_color").isPresent()){
  47. titleText.setTextColor(attrSet.getAttr("title_color").get().getColorValue());
  48. }else{
  49. HiLog.error(LABEL,"attr title_color is not exist");
  50. titleText.setTextColor(Color.BLACK);
  51. }
  52. //处理左边按钮
  53. //获取是否要显示左边按钮
  54. if(attrSet.getAttr("left_button_visible").isPresent()){
  55. if(attrSet.getAttr("left_button_visible").get().getBoolValue()){
  56. leftBtn.setVisibility(VISIBLE);
  57. }else{
  58. leftBtn.setVisibility(INVISIBLE);
  59. }
  60. }else{
  61. //默认情况显示
  62. HiLog.error(LABEL,"attr right_button_visible is not exist");
  63. leftBtn.setVisibility(VISIBLE);
  64. }
  65. //处理左侧按钮的图标
  66. if(attrSet.getAttr("left_button_icon").isPresent()){
  67. leftBtn.setAroundElements(attrSet.getAttr("left_button_icon").get().getElement(),null,null,null);
  68. }else{
  69. HiLog.error(LABEL,"attr left_button_icon is not exist");
  70. }
  71. //处理左侧按钮的文本
  72. if(attrSet.getAttr("left_button_text").isPresent()){
  73. leftBtn.setText(attrSet.getAttr("left_button_text").get().getStringValue());
  74. }else{
  75. HiLog.error(LABEL,"attr left_button_text is not exist");
  76. }
  77. //处理左侧按钮的文本颜色
  78. if(attrSet.getAttr("left_button_text_color").isPresent()){
  79. leftBtn.setTextColor(attrSet.getAttr("left_button_text_color").get().getColorValue());
  80. }else{
  81. HiLog.error(LABEL,"attr left_button_text_color is not exist");
  82. }
  83. //处理左侧按钮的文本大小
  84. if(attrSet.getAttr("left_button_text_size").isPresent()){
  85. leftBtn.setTextSize(attrSet.getAttr("left_button_text_size").get().getIntegerValue(),Text.TextSizeType.FP);
  86. }else{
  87. HiLog.error(LABEL,"attr left_button_text_size is not exist");
  88. }
  89. //处理右边按钮
  90. //获取是否要显示右边按钮
  91. if(attrSet.getAttr("right_button_visible").isPresent()){
  92. if(attrSet.getAttr("right_button_visible").get().getBoolValue()){
  93. rightBtn.setVisibility(VISIBLE);
  94. }else{
  95. rightBtn.setVisibility(INVISIBLE);
  96. }
  97. }else{
  98. //默认情况显示
  99. HiLog.error(LABEL,"attr right_button_visible is not exist");
  100. rightBtn.setVisibility(VISIBLE);
  101. }
  102. //处理右侧按钮的图标
  103. if(attrSet.getAttr("right_button_icon").isPresent()){
  104. rightBtn.setAroundElements(attrSet.getAttr("right_button_icon").get().getElement(),null,null,null);
  105. }else{
  106. HiLog.error(LABEL,"attr right_button_icon is not exist");
  107. }
  108. //处理右侧按钮的文本
  109. if(attrSet.getAttr("right_button_text").isPresent()){
  110. rightBtn.setText(attrSet.getAttr("right_button_text").get().getStringValue());
  111. }else{
  112. HiLog.error(LABEL,"attr right_button_text is not exist");
  113. }
  114. //处理右侧按钮的文本颜色
  115. if(attrSet.getAttr("right_button_text_color").isPresent()){
  116. rightBtn.setTextColor(attrSet.getAttr("right_button_text_color").get().getColorValue());
  117. }else{
  118. HiLog.error(LABEL,"attr right_button_text_color is not exist");
  119. }
  120. //处理右侧按钮的文本大小
  121. if(attrSet.getAttr("right_button_text_size").isPresent()){
  122. rightBtn.setTextSize(attrSet.getAttr("right_button_text_size").get().getIntegerValue(),Text.TextSizeType.FP);
  123. }else{
  124. HiLog.error(LABEL,"attr right_button_text_size is not exist");
  125. }
  126. }
  127. public CustomTitleBar(Context context, AttrSet attrSet, String styleName) {
  128. super(context, attrSet, styleName);
  129. }
  130. }

         这里实现流程和Android中有点类似,但是有个很核心的区别就是没有Android中自定义属性所用到的一个attrs.xml文件中的declare-styleable功能。这里的自定义属性主要通过attrSet.getAttr代码来获取,获取的时候记得做下判断是否存在该属性,判断的api如下

attrSet.getAttr(“bg_color”).isPresent()

到此,该自定义组件就完成了,然后我们使用gradle将其打包成HAR包

通过鸿蒙自定义属性,来创造一个可以为所欲为的自定义标题组件

打包完成之后,会在output中生成一个har包,如下

通过鸿蒙自定义属性,来创造一个可以为所欲为的自定义标题组件

然后将该har包导入到自己的测试项目中的libs目录下,即可调用其中自定义的组件了,如下

通过鸿蒙自定义属性,来创造一个可以为所欲为的自定义标题组件

测试工程的布局代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. xmlns:xdw="http://schemas.huawei.com/res/ohos-auto"
  5. ohos:height="match_parent"
  6. ohos:width="match_parent"
  7. ohos:orientation="vertical">
  8. <com.xdw.mycustomtitlebar.CustomTitleBar
  9. ohos:height="match_content"
  10. ohos:width="match_parent"
  11. xdw:bg_color="$color:blue"
  12. xdw:left_button_visible="false"
  13. xdw:right_button_visible="false"
  14. xdw:title_size="18"
  15. xdw:title_text="这是自定义属性标题"/>
  16. <com.xdw.mycustomtitlebar.CustomTitleBar
  17. ohos:height="45vp"
  18. ohos:width="match_parent"
  19. ohos:top_margin="10vp"
  20. xdw:bg_color="$color:blue"
  21. xdw:left_button_icon="$media:left"
  22. xdw:right_button_icon="$media:add"
  23. xdw:title_color="$color:white"
  24. xdw:title_size="20"
  25. xdw:title_text="标题1"/>
  26. <com.xdw.mycustomtitlebar.CustomTitleBar
  27. ohos:height="45vp"
  28. ohos:width="match_parent"
  29. ohos:top_margin="10vp"
  30. xdw:bg_color="$color:red"
  31. xdw:left_button_icon="$media:left"
  32. xdw:right_button_visible="false"
  33. xdw:title_color="$color:white"
  34. xdw:title_size="20"
  35. xdw:title_text="标题2"/>
  36. <com.xdw.mycustomtitlebar.CustomTitleBar
  37. ohos:height="45vp"
  38. ohos:width="match_parent"
  39. ohos:top_margin="10vp"
  40. xdw:bg_color="$color:red"
  41. xdw:left_button_visible="false"
  42. xdw:right_button_icon="$media:add"
  43. xdw:title_color="$color:white"
  44. xdw:title_size="20"
  45. xdw:title_text="标题3"/>
  46. <com.xdw.mycustomtitlebar.CustomTitleBar
  47. ohos:height="45vp"
  48. ohos:width="match_parent"
  49. ohos:top_margin="10vp"
  50. xdw:bg_color="$color:green"
  51. xdw:left_button_text="左边"
  52. xdw:left_button_text_color="$color:red"
  53. xdw:right_button_icon="$media:add"
  54. xdw:title_color="$color:white"
  55. xdw:title_size="20"
  56. xdw:title_text="标题4"/>
  57. <com.xdw.mycustomtitlebar.CustomTitleBar
  58. ohos:height="45vp"
  59. ohos:width="match_parent"
  60. ohos:top_margin="10vp"
  61. xdw:bg_color="$color:green"
  62. xdw:left_button_text="左边"
  63. xdw:left_button_text_color="$color:red"
  64. xdw:right_button_text="右边"
  65. xdw:right_button_text_color="$color:red"
  66. xdw:title_color="$color:white"
  67. xdw:title_size="20"
  68. xdw:title_text="标题4"/>
  69. </DirectionalLayout>

在布局文件中进行调用的时候需要自定义一个xml命名空间来调用自定义属性,这个命名空间名称和scheme大家都可以随意指定,比如我这里命名空间名称为xdw,后面对应的scheme为”http://schemas.huawei.com/res/ohos-auto”

最后,运行效果图就是本文开头的效果图。目前网上确实没有找到HarmonyOS关于自定义属性这块的博客,所以自己研究了一番发布了此博客,希望能够帮助到大家。

作者:软通夏德旺

想了解更多内容,请访问51CTO和华为合作共建的鸿蒙社区:https://harmonyos.51cto.com/

版权声明:本文为HarmonyOS原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/HarmonyOS/p/14484725.html