赞
踩
作者:韩茹
公司:程序咖(北京)科技有限公司
鸿蒙巴士专栏作家
ToastDialog是在窗口上方弹出的对话框,是通知操作的简单反馈。ToastDialog会在一段时间后消失,在此期间,用户还可以操作当前窗口的其他组件。
所以它的特点:
点击按钮,弹出ToastDialog。
我们现在xml布局文件中:
<?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="vertical"> <Button ohos:id="$+id:btn1" ohos:height="match_content" ohos:width="match_content" ohos:background_element="#EEEEEE" ohos:layout_alignment="horizontal_center" ohos:text="显示一个ToastDialog" ohos:text_size="50px" ohos:top_margin="20vp" ohos:padding="20vp" /> </DirectionalLayout>
布局效果:
在Java代码中:
//点击按钮,弹出ToastDialog
Button btn1 = (Button) findComponentById(ResourceTable.Id_btn1);
btn1.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
new ToastDialog(getContext())
.setText("这是一个吐司对话框")
.show();
}
});
运行效果:
1、设置位置
在xml布局文件中,再添加一个按钮:
<Button
ohos:id="$+id:btn2"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#EEEEEE"
ohos:layout_alignment="horizontal_center"
ohos:text="设置ToastDialog位置"
ohos:text_size="50px"
ohos:top_margin="20vp"
ohos:padding="20vp"
/>
Java中代码:
//2.设置 ToastDialog 的位置 Button btn2 = (Button) findComponentById(ResourceTable.Id_btn2); btn2.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { /* LayoutAlignment.CENTER,居中 LayoutAlignment.LEFT,垂直居中,左侧显示 LayoutAlignment.RIGHT LayoutAlignment.BOTTOM 等等。。 */ new ToastDialog(getContext()) .setText("这个ToastDialog居中显示") .setAlignment(LayoutAlignment.CENTER) .show(); } });
运行效果:
2、自定义ToastDialog的Component
首先我们再添加一个按钮,ability_main.xml布局文件中,继续添加按钮3:
<Button
ohos:id="$+id:btn3"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#EEEEEE"
ohos:layout_alignment="horizontal_center"
ohos:text="自定义ToastDialog"
ohos:text_size="50px"
ohos:top_margin="20vp"
ohos:padding="20vp"
/>
在layout目录下,新建一个布局文件,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:layout_alignment="center" ohos:text_size="16fp" ohos:text="自定义的ToastDialog" ohos:padding="20vp" ohos:background_element="$graphic:background_toast_element"/> </DirectionalLayout>
其中graphic下的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="#33ff0000"/>
</shape>
Java中的代码:
//3.自定义ToastDialog
Button btn3 = (Button) findComponentById(ResourceTable.Id_btn3);
btn3.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
DirectionalLayout toastLayout = (DirectionalLayout) LayoutScatter.getInstance(MainAbilitySlice.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();
}
});
运行效果:
3、自定义ToastDialog带图片
首先我们再添加一个按钮,ability_main.xml布局文件中,继续添加按钮3:
<Button
ohos:id="$+id:btn4"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#EEEEEE"
ohos:layout_alignment="horizontal_center"
ohos:text="带图片的ToastDialog"
ohos:text_size="50px"
ohos:top_margin="20vp"
ohos:padding="20vp"
/>
在layout目录下,新建一个布局文件,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:t4"/> <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="这个一个带图片的ToastDialog" ohos:text_size="16fp" ohos:top_padding="4vp"/> </DirectionalLayout>
这里我们需要将一个图片t4拷贝到media目录下:
Java中的代码:
//4.自定义ToastDialog
Button btn4 = (Button) findComponentById(ResourceTable.Id_btn4);
btn4.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
DirectionalLayout layout = (DirectionalLayout) LayoutScatter.getInstance(MainAbilitySlice.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();
}
});
运行效果:
更多内容:
1、社区:鸿蒙巴士https://www.harmonybus.net/
2、公众号:HarmonyBus
3、技术交流QQ群:714518656
4、视频课:https://www.chengxuka.com
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。