【Java UI】智能手表如何实现负一屏功能

 参考资料

在负一屏展示应用消息

代码实现

xml布局绘画

在resources/base/layout新建一个notification_view.xml,布局文件包含了Image和Text组件,代码如下

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:orientation="horizontal"
    ohos:remote="true">

    <Image
        ohos:height="100fp"
        ohos:width="100fp"
        ohos:image_src="$media:icon"/>

    <Text
        ohos:id="$+id:me_header_text"
        ohos:height="100vp"
        ohos:width="match_parent"
        ohos:text="1111"
        ohos:text_alignment="center"
        ohos:text_color="#ed6262"
        ohos:text_size="24vp"/>

</DirectionalLayout>

java代码实现

在MainAbilitySlice实现相应的代码,详细描述看注释,代码如下

package com.harmony.alliance.myapplication.slice;

import com.harmony.alliance.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.ComponentProvider;
import ohos.agp.utils.Color;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventManager;
import ohos.event.intentagent.IntentAgent;
import ohos.event.intentagent.IntentAgentConstant;
import ohos.event.intentagent.IntentAgentHelper;
import ohos.event.intentagent.IntentAgentInfo;
import ohos.event.notification.NotificationHelper;
import ohos.event.notification.NotificationRequest;
import ohos.rpc.RemoteException;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        //todo 加载布局
        ComponentProvider remoteView = new ComponentProvider(ResourceTable.Layout_notification_view, this);
       //todo 设置文字颜色
        remoteView.setTextColor(ResourceTable.Id_me_header_text, new Color(0xFFFF0000));
      //todo 设置文字大小
        remoteView.setFloat(ResourceTable.Id_me_header_text, "setTextSize", 20);
        //todo 设置文字内容
        remoteView.setString(ResourceTable.Id_me_header_text, "setText", "Normal");
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle("Remoteview Title").setText("Remoteview Text");
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        NotificationRequest request = new NotificationRequest(this, 1);
        //todo 加载布局
        request.setContent(notificationContent).setCustomView(remoteView);
        try {
            //todo 发布负一屏
            NotificationHelper.publishNotification("MinusOne_Ongoing",request);
        } catch (ohos.rpc.RemoteException ex) {
        }
    }

}

运行效果

93dd0698cc98eb7e322401a27fd4f867_748x947.gif%40900-0-90-f.gif

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

posted @ 2022-08-24 09:15  华为开发者论坛  阅读(117)  评论(0编辑  收藏  举报