【Android】自定义dialog

自定义dialog类
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import com.douyu.lib.utils.DYDensityUtils;
import com.douyu.module.screencast.manager.SCCastManager;

import tv.douyu.plugin.demo.SCApplication;
import tv.douyu.plugin.screencast.R;

public class ExitDialog extends AlertDialog {

    private Context mContext;
    private Button btnOk, btnCancel;

    protected ExitDialog(Context context) {
        super(context, R.style.exit_cast_activity_style);
        this.mContext = context;
    }

    protected ExitDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    protected ExitDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(SCApplication.getInstance().getApplicationContext()).inflate(R.layout.sc_exit_dialog, null);
        setContentView(view);
        initView(view);
        initListener();
    }

    private void initListener() {

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //断开连接逻辑

                SCCastManager.getInstance().removeCastControlView();
                SCCastManager.getInstance().exitAllFloatVIew();
                cancel();

            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
            }
        });
    }

    private void initView(View view) {
        btnOk = (Button) view.findViewById(R.id.btn_ok);
        btnCancel = (Button) view.findViewById(R.id.btn_cancel);
    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.format = PixelFormat.TRANSLUCENT;
        layoutParams.width = DYDensityUtils.dip2px(289);
        layoutParams.height = DYDensityUtils.dip2px(181);
        getWindow().setAttributes(layoutParams);
    }
}
dialog样式

调用方式
private void handleClickClose() {
        if (mExitDialog == null){
            mExitDialog = new ExitDialog(mContext);
        }
        mExitDialog.show();
    }
dialog设置宽度全屏,要设置在show的后面
 @Override
    public void show() {
        super.show();
/**
 * 设置宽度全屏,要设置在show的后面
 */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity=Gravity.BOTTOM;
        layoutParams.width= LayoutParams.MATCH_PARENT;
        layoutParams.height= LayoutParams.WRAP_CONTENT;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);
    }

错误不足之处或相关建议欢迎大家评论指出,谢谢!如果觉得内容可以的话麻烦喜欢(♥)一下

你可能感兴趣的:(【Android】自定义dialog)