鸿蒙使用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:background_element="$graphic:background_content"
    ohos:orientation="vertical">

    <Text
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:id="$+id:TitleText"
        ohos:text_color="#FFA1C6F0"
        ohos:text_size="22fp"
        ohos:top_margin="0fp"
        ohos:bottom_margin="10fp"
        ohos:left_padding="20fp"
        ohos:top_padding="10fp"
        ohos:bottom_padding="10fp"
        ohos:text=""
        ohos:background_element="$graphic:background_title"
        ohos:text_alignment="left"
        ohos:layout_alignment="left"></Text>

    <Text
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:id="$+id:ContentText"
        ohos:text_color="#FF05274D"
        ohos:text_size="20fp"
        ohos:top_margin="10fp"
        ohos:bottom_margin="10fp"
        ohos:left_padding="20fp"
        ohos:text=""
        ohos:text_alignment="left"
        ohos:layout_alignment="left"></Text>

    <Button
        ohos:top_margin="10fp"
        ohos:left_margin="20fp"
        ohos:right_margin="20fp"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:id="$+id:CloseCommonDialog"
        ohos:text=""
        ohos:text_color="#fff"
        ohos:text_size="20fp"
        ohos:top_padding="10fp"
        ohos:background_element="$graphic:background_button"
        ohos:bottom_padding="10fp"></Button>

</DirectionalLayout>

二、布局文件添加按钮


打开布局文件

<Button
    ohos:top_margin="10fp"
    ohos:left_margin="20fp"
    ohos:right_margin="20fp"
    ohos:height="match_content"
    ohos:width="match_parent"
    ohos:id="$+id:OpenCommonDialog"
    ohos:text="打开通用对话"
    ohos:text_color="#fff"
    ohos:text_size="20fp"
    ohos:top_padding="10fp"
    ohos:background_element="$graphic:background_button"
    ohos:bottom_padding="10fp"></Button>

三、切片文件录入以下关键代码


打开切片文件

public void onStart(Intent intent) {
    super.onStart(intent);
    super.setUIContent(ResourceTable.Layout_ability_main);
    Button OpenCommonDialog=(Button) findComponentById(ResourceTable.Id_OpenCommonDialog);
    OpenCommonDialog.setClickedListener(clickedListener);
}
public Component.ClickedListener clickedListener= component -> {
    switch (component.getId()){
        case ResourceTable.Id_OpenCommonDialog:
            LoadBaseDialog();
            break;
        default:
            break;
    }
};
public void LoadBaseDialog(){
    TaskDispatcher taskDispatcher=this.getUITaskDispatcher();
    taskDispatcher.asyncDispatch(() -> {
        CommonDialog commonDialog=new CommonDialog(this);
        Component CustomContent=LayoutScatter.getInstance(this).parse(ResourceTable.Layout_common_dialog_content,null,false);
        Text TitleText=(Text) CustomContent.findComponentById(ResourceTable.Id_TitleText);
        TitleText.setText("对话框标题");
        Text ContentText=(Text) CustomContent.findComponentById(ResourceTable.Id_ContentText);
        ContentText.setText("对话框内容");
        Button CloseCommonDialog=(Button) CustomContent.findComponentById(ResourceTable.Id_CloseCommonDialog);
        CloseCommonDialog.setText("对话框按钮");
        CloseCommonDialog.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                commonDialog.destroy();//销毁现有对话框
            }
        });
        //commonDialog.setTitleCustomComponent((DirectionalLayout) CustomTitle);
        commonDialog.setContentCustomComponent(CustomContent);
        commonDialog.setAlignment(LayoutAlignment.CENTER);
        commonDialog.setCornerRadius(50f);
        commonDialog.setSize(DirectionalLayout.LayoutConfig.MATCH_PARENT,600);
        //commonDialog.setSwipeToDismiss(true);//通过在屏幕上向右滑动来退出对话框
        commonDialog.setAutoClosable(true);
        commonDialog.setTransparent(false);
        commonDialog.siteRemovable(true);//设置当用户点击返回键时删除对话框
        commonDialog.show();
    });
}

四、打开鸿蒙手机模拟器运行


鸿蒙手机运行APP效果

举报
评论 0