• 【HarmonyOS】【JAVA UI】自定义通知的实现


     我们怎么实现自定义通知呢?今天写一个demo作为经验分享,我们从以下五个步骤进行描述

    1.      绘画基本界面

    2.     自定义通知的view界面

    3.     代码层实现view的实现设置Text文字,设置Image图片

    4.     运行效果

    在开发中基本通知我们可以参考资料:

    文档中心

    image.png

    第一步:绘画基本界面

    1.1我们在布局绘画一个Text用于触发发布通知 xml代码如下

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <DirectionalLayout
    3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
    4. ohos:height="match_parent"
    5. ohos:width="match_parent"
    6. ohos:orientation="vertical">
    7. <Text
    8. ohos:id="$+id:PushNotification"
    9. ohos:height="100vp"
    10. ohos:width="match_parent"
    11. ohos:background_element="#ed6262"
    12. ohos:layout_alignment="horizontal_center"
    13. ohos:text="发布通知"
    14. ohos:text_color="#ffffff"
    15. ohos:text_alignment="center"
    16. ohos:text_size="40vp"
    17. />
    18. </DirectionalLayout>

    第二步:自定义通知的view界面

    自定义通知布局,新建一个xml 布局名称notification_view.xml 需要设置remote为true

    代码如下

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <DirectionalLayout
    3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
    4. ohos:height="100vp"
    5. ohos:width="match_parent"
    6. ohos:orientation="horizontal"
    7. ohos:remote="true">
    8. <Image
    9. ohos:left_margin="30px"
    10. ohos:id="$+id:myImage"
    11. ohos:height="50vp"
    12. ohos:width="50vp"
    13. ohos:image_src="$media:icon"/>
    14. <DirectionalLayout
    15. ohos:height="match_parent"
    16. ohos:width="match_parent"
    17. ohos:orientation="vertical">
    18. <Text
    19. ohos:id="$+id:title"
    20. ohos:height="0fp"
    21. ohos:weight="1"
    22. ohos:left_padding="20fp"
    23. ohos:text_size="30fp"
    24. ohos:text="通知标题"
    25. ohos:text_alignment="center|left"
    26. ohos:width="match_parent"/>
    27. <Text
    28. ohos:left_padding="20fp"
    29. ohos:height="0fp"
    30. ohos:weight="1"
    31. ohos:text_size="20fp"
    32. ohos:text="通知内容"
    33. ohos:text_alignment="center|left"
    34. ohos:width="match_parent"/>
    35. </DirectionalLayout>
    36. </DirectionalLayout>

    效果如下

    image.png

    第三步 代码层实现view的实现设置Text文字,设置Image图片

    3.1 使用ComponentProvider加载布局代码如下

    ComponentProvider remoteView = new ComponentProvider(ResourceTable.Layout_notification_view, this);

    3.2设置文字

    1. //todo 通知设置文字
    2. remoteView.setTextColor(ResourceTable.Id_title, new Color(0xFFFF0000));
    3. remoteView.setFloat(ResourceTable.Id_title, "setTextSize", 20);
    4. remoteView.setString(ResourceTable.Id_title, "setText", "这是通知标题");

    3.2使用setImageContent的api设置图片资源,

    准备工作

    1)在resources/base/media 文件存放一个图片为myicon.png图片,结构如下

    image.png

    2)我们需要在resources/base/profile文件新建一个名为remote.xml文件,图片如下

    image.png

    3)romote.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <remoteresources>
    3. <item>$media:myicon</item>
    4. </remoteresources>

    4)Java代码设置图片

      remoteView.setImageContent(ResourceTable.Id_myImage, ResourceTable.Media_myicon);

    5)通知代码实现

    1. NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
    2. NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
    3. NotificationRequest request = new NotificationRequest(this, 1);
    4. request.setContent(notificationContent).setCustomView(remoteView);

    6)全部代码如下

    1. public class MainAbilitySlice extends AbilitySlice {
    2. private Text PushNotification;
    3. private String event = "com.utils.test";
    4. static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "######");
    5. @Override
    6. public void onStart(Intent intent) {
    7. super.onStart(intent);
    8. super.setUIContent(ResourceTable.Layout_ability_main);
    9. PushNotification = findComponentById(ResourceTable.Id_PushNotification);
    10. ComponentProvider remoteView = new ComponentProvider(ResourceTable.Layout_notification_view, this);
    11. //todo 通知设置文字
    12. remoteView.setTextColor(ResourceTable.Id_title, new Color(0xFFFF0000));
    13. remoteView.setFloat(ResourceTable.Id_title, "setTextSize", 20);
    14. remoteView.setString(ResourceTable.Id_title, "setText", "这是通知标题");
    15. //todo 通知设置图片
    16. remoteView.setImageContent(ResourceTable.Id_myImage, ResourceTable.Media_myicon);
    17. NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
    18. NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
    19. NotificationRequest request = new NotificationRequest(this, 1);
    20. request.setContent(notificationContent).setCustomView(remoteView);
    21. PushNotification.setClickedListener(new Component.ClickedListener() {
    22. @Override
    23. public void onClick(Component component) {
    24. try {
    25. NotificationHelper.publishNotification(request);
    26. } catch (ohos.rpc.RemoteException ex) {
    27. HiLog.error(LABEL, ex.getLocalizedMessage());
    28. }
    29. }
    30. });
    31. }}

    第四步:效果图如下

    image.png

    更多相关学习资料:
    https://developer.huawei.com/consumer/cn/forum/topic/0201768617076660043?fid=0102683795438680754?ha_source=zzh

  • 相关阅读:
    【2017NOIP普及组】T1:成绩 试题解析
    ssm+vue基本微信小程序的今日菜谱系统
    为什么Redis使用单线程 性能会优于多线程?
    计算机网络概述
    【Debug】NI VISA VI_ERROR_NLISTENERS
    Python升级之路( Lv15 ) 并发编程三剑客: 进程, 线程与协程
    电脑按哪几个键可以录制屏幕?录屏软件与快捷键全攻略
    C++练习:人员信息管理程序计算不同职员的每月工资。
    机器学习笔记 - 关于Contrastive Loss对比损失
    使用神经网络进行预测,图神经网络 社交网络
  • 原文地址:https://blog.csdn.net/weixin_44708240/article/details/125621761