工具篇——RoundCornerDialog(圆角的dialog)

代码如下:

package com.wy.test.other;

import android.app.Dialog;

import android.content.Context;

import android.view.Gravity;

import android.view.View;

import android.view.Window;

import android.view.WindowManager;

/**

* 圆角的dialog

*/

public class RoundCornerDialog extends Dialog {

    private static int default_width = 160; //默认宽度

    private static int default_height = 120;//默认高度

    public RoundCornerDialog(Context context, View layout, int style) {

        this(context, default_width, default_height, layout, style);

    }

    public RoundCornerDialog(Context context, int width, int height, View layout, int style) {

        super(context, style);

        setContentView(layout);

        Window window = getWindow();

        WindowManager.LayoutParams params = window.getAttributes();

        params.gravity = Gravity.CENTER;

        window.setAttributes(params);

    }

    //圆角的dialog样式,在values——》styles中设置

//    

}

在项目中的应用:

1.在values——》styles中设置style样式;




2.在drawable中定义shape_circle_corner_white文件(圆角的背景)





    

    

    

    

    

    

    


3.定义弹出的RoundCornerDialog的layout布局文件;





    

        

        

            

            

        

        

        

    


4.在activity中展示圆角的dialog

/**
 * 展示圆角的dialog
 */
private void showDialog() {
    View view = View.inflate(this, R.layout.dialog_two_btn, null);
    final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(this, 0, 0, view, R.style.RoundCornerDialog);
    roundCornerDialog.show();
    roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
    roundCornerDialog.setOnKeyListener(keylistener);//设置点击返回键Dialog不消失

    TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
    TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
    TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
    tv_message.setText("这是个圆角的dialog");

    //确定
    tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            roundCornerDialog.dismiss();
        }
    });
    //取消
    tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            roundCornerDialog.dismiss();
        }
    });
}

DialogInterface.OnKeyListener keylistener = new DialogInterface.OnKeyListener() {
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            return true;
        } else {
            return false;
        }
    }
};

附:
详情使用Demo(https://blog.csdn.net/qq941263013/article/details/80901277)

你可能感兴趣的:(工具篇——RoundCornerDialog(圆角的dialog))