执行效果java
上一篇文章中说过,直接使用鸿蒙系统中的CommonDialog大体是下面的效果:
git
这个效果实在是没法用于实际的应用开发。本文介绍如何定制本身的CommonDialog。仍是先看演示视频:github
准备布局web
定制CommonDialog的第一步是定义对话框的布局,具体以下:编程
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:width="match_parent" ohos:height="match_content" ohos:alignment="center" ohos:orientation="vertical"> <DirectionalLayout ohos:width="match_content" ohos:height="match_content" ohos:top_padding="10vp" ohos:bottom_padding="10vp" ohos:left_padding="10vp" ohos:right_padding="10vp" ohos:background_element="#FFFFFF" ohos:orientation="vertical"> <Text ohos:width="match_content" ohos:height="match_content" ohos:text="你以为这个对话框怎么样?" ohos:text_color="#000000" ohos:text_size="20fp"/> <DirectionalLayout ohos:height="match_content" ohos:width="match_parent" ohos:top_margin="10vp" ohos:orientation="horizontal"> <Button ohos:id="$+id:good" ohos:width="0vp" ohos:weight = "5" ohos:height="match_content" ohos:text="很好" ohos:text_size="20fp" ohos:text_color="#000000" ohos:background_element="#AAFFAA" /> <Component ohos:width="0vp" ohos:weight = "1" ohos:height="match_parent"/> <Button ohos:id="$+id:ordinary" ohos:width="0vp" ohos:weight = "5" ohos:height="match_content" ohos:text="通常" ohos:text_size="20fp" ohos:text_color="#000000" ohos:background_element="#AAAAFF" /> <Component ohos:width="0vp" ohos:weight = "1" ohos:height="match_parent"/> <Button ohos:id="$+id:bad" ohos:width="0vp" ohos:weight = "5" ohos:height="match_content" ohos:text="很差" ohos:text_size="20fp" ohos:text_color="#000000" ohos:background_element="#FFFF00" /> <Component ohos:width="20vp" ohos:height="match_parent"/> </DirectionalLayout> </DirectionalLayout></DirectionalLayout>
须要注意的是,最外层布局中只包含一个二层布局,这种作法的目的是为了解决对话框默认占满屏幕宽度的问题。
设计模式
生成定制对话框微信
第2行经过LayoutScatter解析前面的布局文件。这种用法在ListContainer示例中使用过。架构
第3行代码将对话框设为透明,实际的效果是让最外层布局透明。视频中展现的从第二层布局开始到内容。
app
CommonDialog dlg = new CommonDialog(this);Component layout = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_common_dialog, null, true);dlg.setTransparent(true);dlg.setContentCustomComponent(layout);Component.ClickedListener listener = new Component.ClickedListener() { public void onClick(Component component) { new ToastDialog(getContext()) .setText(((Button)component).getText()) .show(); }};Button good = (Button)layout.findComponentById(ResourceTable.Id_good);good.setClickedListener(listener);Button ordinary = (Button)layout.findComponentById(ResourceTable.Id_ordinary);ordinary.setClickedListener(listener);Button bad = (Button)layout.findComponentById(ResourceTable.Id_bad);bad.setClickedListener(listener);dlg.show();
若是不调用setTransparent方法的话,画面看起来是下面的样子:ide
第4行代码是经过setContentCustomComponent方法生成你的布局对象传递给CommonDialog。
注意事项
目前的这种作法在鸿蒙文档中并无说明,所以也就不排除未来发生变化的可能性。但愿早日看到官方文档中的正式说法。
参考代码
完整代码能够从如下连接下载:
https://github.com/xueweiguo/Harmony/tree/master/HelloHarmony
参考资料
CommonDialog类
https://developer.harmonyos.com/cn/docs/documentation/doc-references/commondialog-0000001054678727
做者著做介绍
《实战Python设计模式》是做者去年3月份出版的技术书籍,该书利用Python 的标准GUI 工具包tkinter,经过可执行的示例对23 个设计模式逐个进行说明。这样一方面可使读者了解真实的软件开发工做中每一个设计模式的运用场景和想要解决的问题;另外一方面经过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。
对设计模式感兴趣并且但愿随学随用的读者经过本书能够快速跨越从理解到运用的门槛;但愿学习Python GUI 编程的读者能够将本书中的示例做为设计和开发的参考;使用Python 语言进行图像分析、数据处理工做的读者能够直接以本书中的示例为基础,迅速构建本身的系统架构。
以为本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!
本文分享自微信公众号 - 面向对象思考(OOThinkingDalian)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。