HarmonyOS开发Toast

更多技术交流请加入QQ群
群名称:HarmonyOS技术交流
群 号:714886304
ToastDialog是在窗口上方弹出的对话框,是通知操作的简单反馈。ToastDialog会在一段时间后消失,在此期间,用户还可以操作当前窗口的其他组件。

1.创建一个ToastDialog

new ToastDialog(this)
        .setText("This is a ToastDialog")
        .show();

在这里插入图片描述

2.设置位置

new ToastDialog(this)
        .setText("吐司内容")
        .setAlignment(LayoutAlignment.CENTER)
        .show();

在这里插入图片描述

3. 自定义ToastDialog的Component

 DirectionalLayout toastLayout = (DirectionalLayout)
                LayoutScatter.getInstance(this)
                        .parse(ResourceTable.Layout_layout_toast,
                                null, false);
        new ToastDialog(this)
                .setComponent(toastLayout)
                .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT,
                        DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                .setAlignment(LayoutAlignment.CENTER)
                .show();

(1)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="吐司内容"
        ohos:background_element="$graphic:background_toast_element"/>
</DirectionalLayout>

(2)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>

在这里插入图片描述

4. 自定义视图:添加多个视图的场景

(1)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="吐司内容"
        ohos:text_size="16fp"
        ohos:top_padding="4vp"/>
</DirectionalLayout>

(2)其中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>

(3)java代码

DirectionalLayout layout = (DirectionalLayout) LayoutScatter.getInstance(this)
                    .parse(ResourceTable.Layout_layout_toast_and_image, null, false);
new ToastDialog(this)
    .setComponent(layout)
    .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
    .setAlignment(LayoutAlignment.CENTER)
    .show();

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_36158551/article/details/111314701