ToastDialog:在窗口上方提供toast对话框,以通知操作的简单反馈。Toast对话框不可单击,它将自动消失。
本质就是一个对话框(BaseDialog),显示几秒后会自动消失。使用方法和使用对话框没有区别。
常用方法:
//创建ToastDialog
ToastDialog dialog = new ToastDialog(getContext());
//设置ToastDialog显示的位置,默认显示在底部,还可设置显示在顶部,中间,左边,右边
dialog.setAlignment(LayoutAlignment.BOTTOM);
//设置显示的文字,可自定义Component
dialog.setText(msg);
//设置显示时长,测试下来最多2秒,设置的再多也没用
dialog.setDuration(2000);
//设置自定义Component
Component toastLayout = LayoutScatter.getInstance(this)
.parse(ResourceTable.Layout_layout_toast,null,false);
dialog.setComponent(toastLayout);
//设置大小,一般不需要设置
.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
//显示ToastDialog
dialog.show();
//layout_toast代码
<?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: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>
效果图:
自定义Component的ToastDialog
显示文字的ToastDialog
网友评论