当前位置:   article > 正文

【HarmonyOS】自定义组件之JavaUI实现通用标题栏组件_harmonyos settitle

harmonyos settitle

【关键字】

标题栏、常用内置组件整合、JavaUI、自定义组件

【1、写在前面】

平时我们在开发一个应用时,我们都知道一个完整的项目中会有很多个页面,而这些页面中会有许多通用的部分,比如通用标题栏、通用Dialog、通用下拉菜单等等,在Android开发中我们可以通过LayoutInflater.from(Context).inflate(layout,root,false)解析XML布局,从而将多个Android中内置的控件实现一个组合式的自定义View,以达到组件通用化,提高代码的复用性的效果。那么在HarmonyOS应用开发中,同样的也会遇到这种需求,就以标题栏为例,如果每个页面都写一遍,代码既冗余也不美观,所以本着复用的思想,我们需要想办法来解决这个问题,那么在鸿蒙应用开发中该怎么实现这种效果呢,继续往下读,本篇我们的任务就是通过Java语言来实现一个通用的标题栏。

【2、标题栏布局】

首先,我们需要编写一个xml文件,在entry/src/main/resources/base/layout文件夹下新建common_title_layout.xml布局文件,代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DependentLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="48vp"
  5. ohos:width="match_parent"
  6. ohos:background_element="#FF0000">
  7. <Text
  8. ohos:id="$+id:custom_close"
  9. ohos:height="match_content"
  10. ohos:width="60vp"
  11. ohos:align_parent_start="true"
  12. ohos:end_padding="5vp"
  13. ohos:start_padding="5vp"
  14. ohos:text_size="16fp"
  15. ohos:text="关闭"
  16. ohos:vertical_center="true"/>
  17. <Text
  18. ohos:id="$+id:custom_title"
  19. ohos:height="match_content"
  20. ohos:width="60vp"
  21. ohos:text="测试"
  22. ohos:center_in_parent="true"
  23. ohos:text_color="#0000FF"
  24. ohos:text_size="16fp"/>
  25. <Image
  26. ohos:id="$+id:custom_img"
  27. ohos:height="40vp"
  28. ohos:width="40vp"
  29. ohos:align_parent_end="true"
  30. ohos:end_padding="5vp"
  31. ohos:image_src="$media:icon"
  32. ohos:start_padding="5vp"
  33. ohos:vertical_center="true"/>
  34. </DependentLayout>

预览一下效果:

cke_2256.png

【3、标题栏配置】

有了上面的布局文件,接下来我们来实现标题栏的逻辑代码,首先新建一个类CommonTitleBar,然后让该类继承自ComponentContainer,在构造方法中,我们可以通过LayoutScatter这个类来实现类似安卓中LayoutInflater.from(Context).inflate(layout,root,false)解析XML布局的效果,再调用addComponent将解析的结果添加进去,就完成了自定义组合控件的效果了,代码如下所示:

cke_577.png

另外,还可以通过自定义属性的形式对自定义组件中的相关属性进行配置,代码如下:

cke_2884.png

通过上面的代码我们就可以实现在XML布局中类似于使用ohos:bg_color="#FF0000"这种通过配置修改组件属性的效果了。

最后,再来看一下自定义组件中内置组件的事件如何处理呢?这个其实就很简单了,我们可以在类中定义一个返回对应内置组件的方法,然后在实现类中去处理具体的事件:

cke_4946.png

完整代码如下:

  1. public class CommonTitleBar extends ComponentContainer {
  2. private Text close;
  3. private Text title;
  4. private Image img;
  5. public CommonTitleBar(Context context) {
  6. this(context, null);
  7. }
  8. public CommonTitleBar(Context context, AttrSet attrSet) {
  9. super(context, attrSet);
  10. init(context, attrSet);
  11. }
  12. private void init(Context context, AttrSet attrSet) {
  13. Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_common_title_layout, null, false);
  14. close = (Text) component.findComponentById(ResourceTable.Id_custom_close);
  15. title = (Text) component.findComponentById(ResourceTable.Id_custom_title);
  16. img = (Image) component.findComponentById(ResourceTable.Id_custom_img);
  17. addComponent(component);
  18. // 处理整体背景色
  19. if (attrSet.getAttr("bg_color").isPresent()) {
  20. component.setBackground(attrSet.getAttr("bg_color").get().getElement());
  21. }
  22. // 处理返回按钮文字
  23. if (attrSet.getAttr("close_text").isPresent()) {
  24. close.setText(attrSet.getAttr("close_text").get().getStringValue());
  25. } else {
  26. close.setText("<---");
  27. }
  28. // 处理标题文字
  29. if (attrSet.getAttr("title_text").isPresent()) {
  30. title.setText(attrSet.getAttr("title_text").get().getStringValue());
  31. } else {
  32. title.setText("标题");
  33. }
  34. // 处理右侧图片按钮
  35. if (attrSet.getAttr("right_img").isPresent()) {
  36. img.setImageElement(attrSet.getAttr("right_img").get().getElement());
  37. }
  38. }
  39. // 返回左侧关闭按钮
  40. public Text getClose() {
  41. return close;
  42. }
  43. // 返回中间标题
  44. public Text getTitle() {
  45. return title;
  46. }
  47. // 返回右侧菜单按钮
  48. public Image getImg() {
  49. return img;
  50. }
  51. }

【4、使用自定义组件】

通过上面的步骤我们已经完成了自定义组件,下面我们在别的页面中具体的来使用一下这个组件吧。

我们在MainAbilitySlice的布局中引用该组件,打开ability_main.xml这个布局,首先我们需要为自定义组件的自定义属性来创建一个命名空间,名字可以自己取,如下:

cke_19593.png

然后我们就可以通过自定义的这个命名空间来引用上一步在代码中自定义的属性了,需要注意的是,在引入自定义组件时,需要使用自定义组件的全类名,可以选中自定义组件的类,然后鼠标右键,选择Copy Reference选项即可,如下图所示:

cke_24371.png

然后就可以正常使用该组件了,如果使用了自定义属性,那么该属性会按照这里的配置进行修改,如果有的属性没使用到,那么就会按照代码中的默认值进行设置,比如我们可以这样设置:

cke_29444.png

完整的代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. xmlns:jaq="http://schemas.huawei.com/res/ohos-auto"
  5. ohos:height="match_parent"
  6. ohos:width="match_parent"
  7. ohos:alignment="horizontal_center"
  8. ohos:orientation="vertical">
  9. <com.custom.app.view.CommonTitleBar
  10. ohos:id="$+id:title"
  11. ohos:height="48vp"
  12. ohos:width="match_content"
  13. jaq:bg_color="#FF2AD4B2"
  14. jaq:close_text="返回"
  15. jaq:right_img="$media:menu"/>
  16. <Text
  17. ohos:id="$+id:content"
  18. ohos:height="match_parent"
  19. ohos:width="match_parent"
  20. ohos:text="内容"
  21. ohos:text_alignment="center"
  22. ohos:text_size="20fp"/>
  23. </DirectionalLayout>

然后在MainAbilitySlice类中,我们来实现自定义组件中左侧返回按钮的点击事件处理,代码如下:

cke_38614.png

最后实现的效果如下:

cke_45038.png

好了,到这里本篇的内容就介绍完了,下期再会!

 欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/270582
推荐阅读
相关标签
  

闽ICP备14008679号