好吧~dialog

Dialog dialog = new Dialog(RemindContentActivity.this, R.style.MyDialog);
dialog.setContentView(R.layout.remind_dialog);
Window mWindow = dialog.getWindow();
WindowManager.LayoutParams lp = mWindow.getAttributes();
lp.gravity = Gravity.BOTTOM;
lp.width = LayoutParams.MATCH_PARENT;
dialog.getWindow().setAttributes(lp);
dialog.show();

 

 

<?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:orientation="vertical" 
    android:background="@null">
    <Button 
        android:id="@+id/remind_time_once"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#e0ffffff"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="1dp"
        />
    <Button 
        android:id="@+id/remind_time_repeat"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#e0ffffff"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        />
    <Button 
        android:id="@+id/remind_time_cancel"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#e0ffffff"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="20dp"
        />
	
</LinearLayout>

 

 

<style name="MyDialog" parent="@android:Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item> 
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

 

### 代码含义分析 #### 整体概述 此代码是一个头文件(`Dialog.h`),定义了一个继承自`QDialog`类的`Dialog`类,在C++ Qt程序里用于创建对话框窗口。 #### 详细分析 ```cpp #ifndef DIALOG_H #define DIALOG_H ``` 这里运用条件编译,防止头文件被重复包含。若`DIALOG_H`未被定义,就定义它,然后包含下面的代码;要是已定义,就跳过后续代码。 ```cpp #include <QtGui/QDialog> #include <QPushButton> ``` 包含了`QDialog`和`QPushButton`头文件,`QDialog`是对话框窗口的基类,`QPushButton`则用于创建按钮。 ```cpp class Dialog : public QDialog { Q_OBJECT ``` 定义了`Dialog`类,它继承自`QDialog`。`Q_OBJECT`宏是Qt元对象系统的关键,用于支持信号和槽机制。 ```cpp protected: QPushButton ModalBtn; QPushButton NormalBtn; QPushButton MixedBtn; ``` 声明了三个受保护的`QPushButton`对象,分别是`ModalBtn`、`NormalBtn`和`MixedBtn`,这三个按钮会在对话框中使用。 ```cpp protected slots: void ModalBtn_Clicked(); void NormalBtn_Clicked(); void MixedBtn_Clicked(); ``` 声明了三个受保护的槽函数,当对应的按钮被点击时,这些槽函数会被调用。 ```cpp public: Dialog(QWidget *parent = 0); ~Dialog(); ``` 声明了构造函数和析构函数。构造函数接收一个`QWidget`类型的指针作为父窗口,默认值为`0`;析构函数用于释放对象占用的资源。 ```cpp #endif // DIALOG_H ``` 结束条件编译块。 ### 可能存在的问题 #### 1. 缺少构造函数和析构函数的实现 头文件里仅声明了构造函数和析构函数,却没有给出具体实现。在对应的源文件(`Dialog.cpp`)中需要实现这些函数。 #### 2. 槽函数未实现 头文件里声明了槽函数,但未给出实现。在源文件中需要实现这些槽函数,以处理按钮点击事件。 #### 3. 按钮未初始化和连接信号与槽 代码里没有对按钮进行初始化,也没有将按钮的`clicked()`信号连接到对应的槽函数。需要在构造函数中完成这些操作。 #### 示例优化代码 以下是`Dialog.cpp`文件的示例实现: ```cpp #include "Dialog.h" #include <QVBoxLayout> Dialog::Dialog(QWidget *parent) : QDialog(parent) { // 初始化按钮 ModalBtn.setText("Modal Button"); NormalBtn.setText("Normal Button"); MixedBtn.setText("Mixed Button"); // 创建布局 QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(&ModalBtn); layout->addWidget(&NormalBtn); layout->addWidget(&MixedBtn); setLayout(layout); // 连接信号和槽 connect(&ModalBtn, &QPushButton::clicked, this, &Dialog::ModalBtn_Clicked); connect(&NormalBtn, &QPushButton::clicked, this, &Dialog::NormalBtn_Clicked); connect(&MixedBtn, &QPushButton::clicked, this, &Dialog::MixedBtn_Clicked); } Dialog::~Dialog() { // 析构函数可以为空,因为Qt会自动管理对象的生命周期 } void Dialog::ModalBtn_Clicked() { // 处理模态按钮点击事件 } void Dialog::NormalBtn_Clicked() { // 处理普通按钮点击事件 } void Dialog::MixedBtn_Clicked() { // 处理混合按钮点击事件 } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值