Android Dialog的封装

这篇博客主要介绍了如何在Android中进行Dialog的封装,包括初始化、设置点击事件和弹出位置。通过Window的setGravity方法控制Dialog的位置。封装后的Dialog类提供了方便的set方法和接口。此外,还讲解了如何添加弹出动画,如dialog_enter和dialog_exit的XML动画文件,并建议在创建Dialog时使用自定义样式。
摘要由CSDN通过智能技术生成

效果
这里写图片描述
常规使用:

                Dialog dialog=new Dialog(this,R.style.DialogStyle);
                View dilaogView = LayoutInflater.from(this).inflate(
                        R.layout.dialog_diylayout, null);
                dialog.setContentView(dilaogView);
                dialog.show();

如果需要设置dialog的弹出位置,则需要用到Window,通过setGravity来控制dialog位置

        Dialog  dialog = new Dialog(context, R.style.DialogStyle);
        Window win = dialog.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        // 设置弹出框的宽高
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        // 设置弹出框的位置
        win.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        win.setAttributes(lp);
        win.setContentView(R.layout
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个基础的 Android Dialog 使用封装的示例代码,可以作为参考: ```java public class BaseDialog extends Dialog { public BaseDialog(@NonNull Context context) { super(context); init(); } public BaseDialog(@NonNull Context context, int themeResId) { super(context, themeResId); init(); } private void init() { // 设置 Dialog 样式等 requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); setCancelable(true); setCanceledOnTouchOutside(true); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_layout); // 初始化 Dialog 布局和控件 initView(); } /** * 初始化 Dialog 布局和控件 */ private void initView() { // TODO: 初始化 Dialog 布局和控件 } /** * 显示 Dialog */ public void showDialog() { if (!isShowing()) { show(); } } /** * 隐藏 Dialog */ public void hideDialog() { if (isShowing()) { dismiss(); } } } ``` 在这个示例中,`BaseDialog` 是一个基础的 Dialog 类,它继承自 `Dialog` 类,并且对 Dialog 进行了一些基础的封装。在 `init()` 方法中,我们设置了 Dialog 的样式等属性;在 `onCreate()` 方法中,我们初始化了 Dialog 的布局和控件;在 `showDialog()` 和 `hideDialog()` 方法中,我们分别实现了显示和隐藏 Dialog 的逻辑。 使用这个基础的 Dialog 类,我们只需要继承它,并实现 `initView()` 方法来初始化自定义 Dialog 的布局和控件即可。例如: ```java public class MyDialog extends BaseDialog { public MyDialog(@NonNull Context context) { super(context); } public MyDialog(@NonNull Context context, int themeResId) { super(context, themeResId); } @Override protected void initView() { // 初始化 Dialog 布局和控件 TextView titleView = findViewById(R.id.dialog_title); Button confirmBtn = findViewById(R.id.dialog_confirm_btn); // 设置 Dialog 标题和按钮点击事件等 titleView.setText("这是一个自定义 Dialog"); confirmBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO: 处理确认按钮点击事件 hideDialog(); } }); } } ``` 在这个示例中,`MyDialog` 继承自 `BaseDialog` 类,并且实现了 `initView()` 方法来初始化自定义 Dialog 的布局和控件。在 `initView()` 方法中,我们可以通过 `findViewById()` 方法获取 Dialog 中的控件,并设置它们的属性和事件等。这样,我们就可以通过 `MyDialog` 类来创建和显示自定义Dialog 了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值