推荐:
日常工作中,自己定义了 Dialog 的工具类,以便使用,下面从屏幕中间弹出的 Dialog,效果图如下:
下面主要讲的是:
- 自定义 Dialog 工具类
- 自定义布局文件
- 自定义 Dialog 显示风格
- 显示/关闭 Dialog(使用自定义 dialog)
1.自定义 Dialog 工具类
定义一个名字为 DialogUtil 的工具类,代码如下:
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
/**
* author: wu
* date: on 2018/7/5.
* describe:自定义dialog工具类
*/
public class DialogUtil {
private Dialog dialog;
private View inflate;
//中间显示的dialog
public void showCentreDialog(Context context) {
//自定义dialog显示布局
inflate = LayoutInflater.from(context).inflate(R.layout.dialog_centre, null);
//自定义dialog显示风格
dialog = new Dialog(context, R.style.DialogCentre);
//点击其他区域消失
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(inflate);
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.CENTER;
wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(wlp);
dialog.show();
}
//关闭dia