Android深度自定义Dialog对话框

来源:互联网 发布:2017淘宝卖家用什么app 编辑:程序博客网 时间:2024/05/17 00:54

效果图
这里写图片描述
源码链接:点我下载
功能:

SettingDialog msettingDialog = new SettingDialog(this, R.style.IdealDialog);//设置长宽msettingDialog.setSize(600,600);//设置位置msettingDialog.setLocation(150,200);//设置背景透明度msettingDialog.setAlpha(0.8f);//设置点击事件回调函数msettingDialog.setSettingDialogCallBack(new SettingDialogCallBack(){    @Override    public void onActionClick(int id) {        // TODO Auto-generated method stub        switch(id){        case SettingDialog.BN_MINE:        case SettingDialog.BN_WIFI:        case SettingDialog.BN_QR:        case SettingDialog.BN_CLOSE:        }    }});msettingDialog.show();

补充:该自定义对话框的布局方式采用按比例切割,所以在设置对话框的大小和位置的时候保证不会变形。
实现类全部代码预览:

package com.example.dialog;import com.example.diolog.R;import android.app.Dialog;import android.content.Context;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.widget.ImageButton;import android.widget.RelativeLayout;/** * 设置对话框 * <br>Copyright (c) 2015 双华科技 * @author YHD * @version 2015-10-15 下午5:00:20 */public class SettingDialog extends Dialog {    /** 我的按键id */    public static final int BN_MINE = 0;    /** wifi连接按键id */    public static final int BN_WIFI = 1;    /** 二维码生成id */    public static final int BN_QR = 2;    /** 设置窗口关闭按键id */    public static final int BN_CLOSE = 3;    private float alpha;    private int height;    private int width;    private int x;    private int y;    private ImageButton mine=null;    private ImageButton wifi_connect=null;    private ImageButton qr_create=null;    private ImageButton close=null;    private SettingDialogCallBack settingDialogCallBack;    public SettingDialog(Context context,int theme) {        super(context, theme);        this.alpha=0.5f;        this.width=800;        this.height=800;        this.x=0;        this.y=0;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.dialog_setting);        initUI();        setAlpha();        setSize();        setLocation();    }    /**     * 设置点击回调     * @param settingDialogCallBack     */    public void setSettingDialogCallBack (SettingDialogCallBack settingDialogCallBack) {        this.settingDialogCallBack = settingDialogCallBack;    }    /**     * 设置背景透明度     * @param Alpha 透明度     */    public void setAlpha(float Alpha) {        alpha=Alpha;    }    /**     * 设置对话框大小     * @param wight      * @param height     */    public void setSize(int wight,int height) {        this.width=wight;        this.height=height;    }    /**     * 设置对话框显示位置     * @param x     * @param y     */    public void setLocation(int x,int y) {        this.x=x;        this.y=y;    }    private void initUI(){        mine= (ImageButton)findViewById(R.id.mine);        mine.setOnClickListener(new View.OnClickListener (){            @Override            public void onClick(View v) {                settingDialogCallBack.onActionClick(BN_MINE);            }        });        wifi_connect= (ImageButton)findViewById(R.id.wifi_connect);        wifi_connect.setOnClickListener(new View.OnClickListener (){            @Override            public void onClick(View v) {                settingDialogCallBack.onActionClick(BN_WIFI);            }        });        qr_create= (ImageButton)findViewById(R.id.qr_create);        qr_create.setOnClickListener(new View.OnClickListener (){            @Override            public void onClick(View v) {                settingDialogCallBack.onActionClick(BN_QR);            }        });        close= (ImageButton)findViewById(R.id.close);        close.setOnClickListener(new View.OnClickListener (){            @Override            public void onClick(View v) {                settingDialogCallBack.onActionClick(BN_CLOSE);            }        });    }    private void setAlpha(){        WindowManager.LayoutParams lp=this.getWindow().getAttributes();         lp.dimAmount=alpha;        this.getWindow().setAttributes(lp);         this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);    }      private void setSize(){        RelativeLayout ll=(RelativeLayout)this.findViewById(R.id.linearlayout_whole);        android.view.ViewGroup.LayoutParams lp =ll.getLayoutParams();        lp.height=height;        lp.width=width;    }    private void setLocation() {        Window dialogWindow = this.getWindow();        WindowManager.LayoutParams lp = dialogWindow.getAttributes();        dialogWindow.setGravity(Gravity.LEFT | Gravity.TOP);        lp.x = x;        lp.y = y;         dialogWindow.setAttributes(lp);    }    /**     * 监听按键点击回调接口     * <br>Copyright (c) 2015 双华科技     * @author YHD     * @version 2015-10-15 下午5:02:45     */    public interface SettingDialogCallBack {            /**         * 事件点击         * @param id 返回点击按键的id         */        public void onActionClick (int id);    }}
1 0
原创粉丝点击