Android 对话框 - 对话框全屏显示(设置 Window 属性、使用自定义样式、继承 DialogFragment 实现、继承 Dialog 实现)

一、设置 Window 属性

1、Dialog
  • custom_dialog.xml
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingStart="20dp"
    android:paddingTop="40dp"
    android:paddingEnd="20dp"
    android:paddingBottom="40dp">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="title"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/tv_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="message"
        android:textSize="20dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <android.widget.Button
            android:id="@+id/btn_confirm"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="confirm"
            android:textColor="@color/black" />

        <android.widget.Button
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="cancel"
            android:textColor="@color/black" />
    </LinearLayout>
</LinearLayout>
2、Test
java 复制代码
Dialog dialog = new Dialog(this);

dialog.setContentView(R.layout.custom_dialog);

TextView tvTitle = dialog.findViewById(R.id.tv_title);
TextView tvMessage = dialog.findViewById(R.id.tv_message);
Button btnConfirm = dialog.findViewById(R.id.btn_confirm);
Button btnCancel = dialog.findViewById(R.id.btn_cancel);
tvTitle.setText("系统提示");
tvMessage.setText("确定退出吗?");
btnConfirm.setOnClickListener(v -> {
    Toast.makeText(this, "点击了确定", Toast.LENGTH_SHORT).show();
    dialog.dismiss();
});
btnCancel.setOnClickListener(v -> {
    Toast.makeText(this, "点击了取消", Toast.LENGTH_SHORT).show();
    dialog.dismiss();
});

Window window = dialog.getWindow();
if (window != null) {
    window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}

dialog.show();
  • 另一种设置 Window 属性的方式
java 复制代码
Dialog dialog = new Dialog(this);

dialog.setContentView(R.layout.custom_dialog);

TextView tvTitle = dialog.findViewById(R.id.tv_title);
TextView tvMessage = dialog.findViewById(R.id.tv_message);
Button btnConfirm = dialog.findViewById(R.id.btn_confirm);
Button btnCancel = dialog.findViewById(R.id.btn_cancel);
tvTitle.setText("系统提示");
tvMessage.setText("确定退出吗?");
btnConfirm.setOnClickListener(v -> {
    Toast.makeText(this, "点击了确定", Toast.LENGTH_SHORT).show();
    dialog.dismiss();
});
btnCancel.setOnClickListener(v -> {
    Toast.makeText(this, "点击了取消", Toast.LENGTH_SHORT).show();
    dialog.dismiss();
});

Window window = dialog.getWindow();
if (window != null) {
    WindowManager.LayoutParams params = window.getAttributes();
    params.width = WindowManager.LayoutParams.MATCH_PARENT;
    params.height = WindowManager.LayoutParams.MATCH_PARENT;
    window.setAttributes(params);
}

dialog.show();
xml 复制代码
<style name="FullScreenDialog" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">false</item>
</style>

二、使用自定义样式

1、Style
  • res/values/styles.xml
xml 复制代码
<style name="FullScreenDialog" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">false</item>
</style>
  1. windowFullscreen 设置为 true 强制 Dialog 进入全屏模式

  2. windowIsFloating 设置为 false 告诉系统这个 Dialog 不悬浮,可以占据整个屏幕

2、Dialog
  • custom_dialog.xml
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingStart="20dp"
    android:paddingTop="40dp"
    android:paddingEnd="20dp"
    android:paddingBottom="40dp">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="title"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/tv_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="message"
        android:textSize="20dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <android.widget.Button
            android:id="@+id/btn_confirm"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="confirm"
            android:textColor="@color/black" />

        <android.widget.Button
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="cancel"
            android:textColor="@color/black" />
    </LinearLayout>
</LinearLayout>
3、Test
java 复制代码
Dialog dialog = new Dialog(this, R.style.FullScreenDialog);

dialog.setContentView(R.layout.custom_dialog);

TextView tvTitle = dialog.findViewById(R.id.tv_title);
TextView tvMessage = dialog.findViewById(R.id.tv_message);
Button btnConfirm = dialog.findViewById(R.id.btn_confirm);
Button btnCancel = dialog.findViewById(R.id.btn_cancel);
tvTitle.setText("系统提示");
tvMessage.setText("确定退出吗?");
btnConfirm.setOnClickListener(v -> {
    Toast.makeText(this, "点击了确定", Toast.LENGTH_SHORT).show();
    dialog.dismiss();
});
btnCancel.setOnClickListener(v -> {
    Toast.makeText(this, "点击了取消", Toast.LENGTH_SHORT).show();
    dialog.dismiss();
});

dialog.show();

三、继承 DialogFragment 实现

1、Dialog
  • custom_dialog.xml
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingStart="20dp"
    android:paddingTop="40dp"
    android:paddingEnd="20dp"
    android:paddingBottom="40dp">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="title"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/tv_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="message"
        android:textSize="20dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <android.widget.Button
            android:id="@+id/btn_confirm"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="confirm"
            android:textColor="@color/black" />

        <android.widget.Button
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="cancel"
            android:textColor="@color/black" />
    </LinearLayout>
</LinearLayout>
  • FullScreenDialogFragment.java
java 复制代码
public class FullScreenDialogFragment extends DialogFragment {

    private FragmentActivity fragmentActivity;

    private TextView tvTitle;
    private TextView tvMessage;
    private Button btnConfirm;
    private Button btnCancel;

    private String titleStr;
    private String messageStr;
    private String confirmStr;
    private String cancelStr;

    private OnConfirmClickObserver onConfirmClickObserver;
    private OnCancelClickObserver onCancelClickObserver;

    public FullScreenDialogFragment(FragmentActivity fragmentActivity,
                                    String titleStr,
                                    String messageStr,
                                    String confirmStr,
                                    String cancelStr,
                                    OnConfirmClickObserver onConfirmClickObserver,
                                    OnCancelClickObserver onCancelClickObserver) {
        this.fragmentActivity = fragmentActivity;
        this.titleStr = titleStr;
        this.messageStr = messageStr;
        this.confirmStr = confirmStr;
        this.cancelStr = cancelStr;
        this.onConfirmClickObserver = onConfirmClickObserver;
        this.onCancelClickObserver = onCancelClickObserver;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.custom_dialog, container, false);

        tvTitle = view.findViewById(R.id.tv_title);
        tvMessage = view.findViewById(R.id.tv_message);
        btnConfirm = view.findViewById(R.id.btn_confirm);
        btnCancel = view.findViewById(R.id.btn_cancel);

        tvTitle.setText(titleStr);
        tvMessage.setText(messageStr);
        btnConfirm.setText(confirmStr);
        btnCancel.setText(cancelStr);

        btnConfirm.setOnClickListener(v -> {
            if (onConfirmClickObserver != null) onConfirmClickObserver.confirm();
            dismiss();
        });

        btnCancel.setOnClickListener(v -> {
            if (onCancelClickObserver != null) onCancelClickObserver.cancel();
            dismiss();
        });

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

        Dialog dialog = getDialog();
        if (dialog == null) return;
        Window window = dialog.getWindow();
        if (window == null) return;
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    public void show() {
        super.show(fragmentActivity.getSupportFragmentManager(), "");
    }
}
2、Test
java 复制代码
FullScreenDialogFragment dialogFragment = new FullScreenDialogFragment(
        this,
        "系统提示",
        "确定退出吗?",
        "确定",
        "取消",
        () -> {
            Toast.makeText(this, "点击了确定", Toast.LENGTH_SHORT).show();
        },
        () -> {
            Toast.makeText(this, "点击了取消", Toast.LENGTH_SHORT).show();
        });

dialogFragment.show();

四、继承 Dialog 实现

1、Dialog
  • custom_dialog.xml
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingStart="20dp"
    android:paddingTop="40dp"
    android:paddingEnd="20dp"
    android:paddingBottom="40dp">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="title"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/tv_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="message"
        android:textSize="20dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <android.widget.Button
            android:id="@+id/btn_confirm"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="confirm"
            android:textColor="@color/black" />

        <android.widget.Button
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="cancel"
            android:textColor="@color/black" />
    </LinearLayout>
</LinearLayout>
  • FullScreenDialog.java
java 复制代码
public class FullScreenDialog extends Dialog {

    private TextView tvTitle;
    private TextView tvMessage;
    private Button btnConfirm;
    private Button btnCancel;

    private String titleStr;
    private String messageStr;
    private String confirmStr;
    private String cancelStr;

    private OnConfirmClickObserver onConfirmClickObserver;
    private OnCancelClickObserver onCancelClickObserver;

    public FullScreenDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.custom_dialog);

        tvTitle = findViewById(R.id.tv_title);
        tvMessage = findViewById(R.id.tv_message);
        btnConfirm = findViewById(R.id.btn_confirm);
        btnCancel = findViewById(R.id.btn_cancel);

        tvTitle.setText(titleStr);
        tvMessage.setText(messageStr);
        btnConfirm.setText(confirmStr);
        btnCancel.setText(cancelStr);

        btnConfirm.setOnClickListener(view -> {
            if (onConfirmClickObserver != null) onConfirmClickObserver.confirm();
            dismiss();
        });

        btnCancel.setOnClickListener(view -> {
            if (onCancelClickObserver != null) onCancelClickObserver.cancel();
            dismiss();
        });

        Window window = getWindow();
        if (window == null) return;
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    public void setTitle(String str) {
        this.titleStr = str == null ? "" : str;
    }

    public void setMessage(String str) {
        this.messageStr = str == null ? "" : str;
    }

    public void setConfirm(String str, OnConfirmClickObserver onConfirmClickObserver) {
        this.confirmStr = str == null ? "" : str;
        this.onConfirmClickObserver = onConfirmClickObserver;
    }

    public void setCancel(String str, OnCancelClickObserver onCancelClickObserver) {
        this.cancelStr = str == null ? "" : str;
        this.onCancelClickObserver = onCancelClickObserver;
    }
}
2、Test
java 复制代码
FullScreenDialog fullScreenDialog = new FullScreenDialog(this);

fullScreenDialog.setTitle("系统提示");
fullScreenDialog.setMessage("确定退出吗?");
fullScreenDialog.setConfirm("确定", () -> {
    Toast.makeText(this, "点击了确定", Toast.LENGTH_SHORT).show();
});
fullScreenDialog.setCancel("取消", () -> {
    Toast.makeText(this, "点击了取消", Toast.LENGTH_SHORT).show();
});

fullScreenDialog.show();
相关推荐
weixin_460443565 小时前
2026年新修订《网络安全法》实施:Java私有化考试系统如何做好SBOM、漏洞治理与安全升级?
java·开发语言·在线考试系统·企业培训考试系统·宏远培训考试系统·国企培训考试系统·国产化培训系统
海鸥-w8 小时前
什么是AOP
java
Cc.Y8 小时前
Java零基础入门:可变字符串与包装类:StringBuilder、StringBuffer 与 通讯录管理系统实战
java·开发语言·python
paopaokaka_luck8 小时前
基于springboot+vue的智慧家政管理系统(AI问答、 WebSocket实时沟通、ECharts运营分析、订单规则与服务追踪)
java·javascript·spring boot·数据分析·echarts·mybatis
sm_926787059 小时前
RFID 标签打印的技术实现要点与二次开发实践
java·大数据·前端·c++·编辑器
阿森_dev9 小时前
2027 届毕设选题怎么选:30 个 Java 方向选题的实现难度排序
java·开发语言·spring boot·毕业设计·课程设计
Sarvartha9 小时前
Lambda 表达式和函数式接口
java·开发语言
计算机毕业设计杰瑞9 小时前
【计算机毕设精品】基于SpringBoot的一体化教务与学业质量管理系统的设计与实现,附源码_增删改查_java项目_可完美运行部署_网站项目
java·spring boot·课程设计
colinu9 小时前
C++ 异步任务与线程生命周期:`std::async`、`future`、`thread`、`jthread` 与协作式退出
java·jvm·c++
大圣编蚕9 小时前
初探 NDK 的世界:从零开始理解 Android 原生开发
android