HarmonyOS之常用组件ToastDialog的功能和使用

一、ToastDialog 的使用
  • ToastDialog 是在窗口上方弹出的对话框,是通知操作的简单反馈。ToastDialog 会在一段时间后消失,在此期间,用户还可以操作当前窗口的其他组件。
  • 创建一个 ToastDialog:
	new ToastDialog(getContext())
	        .setText("This is a ToastDialog")
	        .show();
    • ToastDialog 效果图如下:

在这里插入图片描述

  • 设置位置:
	new ToastDialog(getContext())
	    .setText("This is a ToastDialog displayed in the middle")
	    .setAlignment(LayoutAlignment.CENTER)
	    .show();
    • 设置显示位置的效果:

在这里插入图片描述

  • 自定义 ToastDialog 的 Component:
    • Java 代码示例:
	DirectionalLayout toastLayout = (DirectionalLayout) LayoutScatter.getInstance(this)
	                    .parse(ResourceTable.Layout_layout_toast, null, false);
	new ToastDialog(getContext())
	    .setContentCustomComponent(toastLayout)
	    .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
	    .setAlignment(LayoutAlignment.CENTER)
	    .show();
    • layout_toast.xml 布局:
	<?xml version="1.0" encoding="utf-8"?>
	<DirectionalLayout
	    xmlns:ohos="http://schemas.huawei.com/res/ohos"
	    ohos:height="match_content"
	    ohos:width="match_content"
	    ohos:orientation="vertical">
	    <Text
	        ohos:id="$+id:msg_toast"
	        ohos:height="match_content"
	        ohos:width="match_content"
	        ohos:left_padding="16vp"
	        ohos:right_padding="16vp"
	        ohos:top_padding="4vp"
	        ohos:bottom_padding="4vp"
	        ohos:layout_alignment="center"
	        ohos:text_size="16fp"
	        ohos:text="This is a ToastDialog for the customized component"
	        ohos:background_element="$graphic:background_toast_element"/>
	</DirectionalLayout>    
    • 其中 background_toast_element.xml:
	<?xml version="1.0" encoding="utf-8"?>
	<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
	       ohos:shape="rectangle">
	    <corners
	        ohos:radius="30vp"/>
	    <solid
	        ohos:color="#66808080"/>
	</shape>
    • 自定义 Component 效果:

在这里插入图片描述

二、场景示例
  • 自定义视图:添加多个视图的场景。自定义多个组件的效果如下:

在这里插入图片描述

  • layout_toast_and_image.xml 代码示例:
	<?xml version="1.0" encoding="utf-8"?>
	<DirectionalLayout
	    xmlns:ohos="http://schemas.huawei.com/res/ohos"
	    ohos:height="match_content"
	    ohos:width="match_content"
	    ohos:orientation="horizontal">
	
	    <Image
	        ohos:width="30vp"
	        ohos:height="30vp"
	        ohos:scale_mode="inside"
	        ohos:image_src="$media:icon"/>
	
	    <Text
	        ohos:id="$+id:msg_toast"
	        ohos:height="match_content"
	        ohos:width="match_content"
	        ohos:background_element="$graphic:background_toast_element"
	        ohos:bottom_padding="4vp"
	        ohos:layout_alignment="vertical_center"
	        ohos:left_padding="16vp"
	        ohos:right_padding="16vp"
	        ohos:text="This is a ToastDialog with An Image"
	        ohos:text_size="16fp"
	        ohos:top_padding="4vp"/>
	</DirectionalLayout>
    • 其中 background_toast_element.xml:
	<?xml version="1.0" encoding="utf-8"?>
	<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
	       ohos:shape="rectangle">
	    <corners
	        ohos:radius="30vp"/>
	    <solid
	        ohos:color="#66808080"/>
	</shape>
    • Java 示例代码:
	DirectionalLayout layout = (DirectionalLayout) LayoutScatter.getInstance(this)
	                    .parse(ResourceTable.Layout_layout_toast_and_image, null, false);
	new ToastDialog(getContext())
	    .setContentCustomComponent(layout)
	    .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
	    .setAlignment(LayoutAlignment.CENTER)
	    .show();
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

╰つ栺尖篴夢ゞ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值