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

开发
文章由鸿蒙社区产出,想要了解更多内容请前往:51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com

[[386406]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

之前已经写过一个在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.  
  7.     <Button 
  8.         ohos:id="$+id:title_bar_left" 
  9.         ohos:height="match_content" 
  10.         ohos:width="match_content" 
  11.         ohos:align_parent_start="true" 
  12.         ohos:left_padding="5vp" 
  13.         ohos:min_height="45vp" 
  14.         ohos:min_width="45vp" 
  15.         ohos:text_size="14fp" 
  16.         ohos:vertical_center="true"/> 
  17.  
  18.     <Text 
  19.         ohos:id="$+id:titleText" 
  20.         ohos:height="match_content" 
  21.         ohos:width="match_content" 
  22.         ohos:center_in_parent="true" 
  23.         ohos:multiple_lines="false" 
  24.         ohos:text_size="17fp"/> 
  25.  
  26.     <Button 
  27.         ohos:id="$+id:title_bar_right" 
  28.         ohos:height="match_content" 
  29.         ohos:width="match_content" 
  30.         ohos:align_parent_end="true" 
  31.         ohos:left_padding="5vp" 
  32.         ohos:min_height="45vp" 
  33.         ohos:min_width="45vp" 
  34.         ohos:right_margin="5vp" 
  35.         ohos:text_size="14fp" 
  36.         ohos:vertical_center="true"/> 
  37. </DependentLayout> 

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

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

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

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

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

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

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2013-04-01 14:35:10

Android开发Android自定义x

2022-04-24 15:17:56

鸿蒙操作系统

2022-10-25 15:12:24

自定义组件鸿蒙

2022-10-26 15:54:46

canvas组件鸿蒙

2012-11-19 11:07:42

IBMdw

2021-10-26 10:07:02

鸿蒙HarmonyOS应用

2015-02-12 15:33:43

微信SDK

2022-09-21 14:42:03

JSProps属性

2023-02-20 15:20:43

启动页组件鸿蒙

2009-06-24 15:13:36

自定义JSF组件

2022-06-06 09:28:36

ReactHook

2015-02-12 15:38:26

微信SDK

2009-08-03 13:32:38

C#自定义组件

2021-11-01 10:21:36

鸿蒙HarmonyOS应用

2023-01-03 07:40:27

自定义滑块组件

2021-09-15 10:19:15

鸿蒙HarmonyOS应用

2022-07-15 16:45:35

slider滑块组件鸿蒙

2022-12-07 08:56:27

SpringMVC核心组件

2022-03-01 16:09:06

OpenHarmon鸿蒙单选组件

2009-06-25 14:53:35

自定义UI组件JSF框架
点赞
收藏

51CTO技术栈公众号