对话框自定义:相对布局的java代码实现
创建AlertDiaglog

Window window = dlg.getWindow()

创建布局,代码为相对布局

载入布局,载入相关空间,设置相关控件的位置

代码如下

        int bgImageViewID = 10;
        int iconImageViewID = 11;
        int textViewID = 12;
        int buttonOkID = 13;
        int buttonCancelID = 14;
        int srcImageViewId = 15;
        
        final AlertDialog dlg = new AlertDialog.Builder(this).create();
        dlg.show();
        Window window = dlg.getWindow();
        // 主要就是在这里实现这种效果的.    
        RelativeLayout rLayout = new RelativeLayout(this); 
        //设置背景
        ImageView bg_iv = new ImageView(this);
        bg_iv.setId(bgImageViewID);
        bg_iv.setImageResource(R.drawable.bg2);
        
        RelativeLayout.LayoutParams bgRelativeParams_iv = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        bgRelativeParams_iv.addRule(RelativeLayout.CENTER_HORIZONTAL);
        rLayout.addView(bg_iv, bgRelativeParams_iv);
        
        ImageView icon_iv = new ImageView(this);
        icon_iv.setId(iconImageViewID);
        icon_iv.setImageResource(R.drawable.egame_logo_icon);
        
        RelativeLayout.LayoutParams iconRelativeParams_iv = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        
        iconRelativeParams_iv.addRule(RelativeLayout.ALIGN_LEFT,bgImageViewID);
        iconRelativeParams_iv.addRule(RelativeLayout.ALIGN_TOP,bgImageViewID);
        rLayout.addView(icon_iv, iconRelativeParams_iv);
        
        
        TextView tv = new TextView(this);
        tv.setId(textViewID);
        tv.setText("XXXXXX");
    
        RelativeLayout.LayoutParams RelativeParams_tv = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    
        RelativeParams_tv.addRule(RelativeLayout.CENTER_HORIZONTAL,iconImageViewID);
        rLayout.addView(tv, RelativeParams_tv);
                
        ImageView src_iv = new ImageView(this);
        src_iv.setId(srcImageViewId);
        src_iv.setBackgroundResource(R.drawable.app_icon);
    
        RelativeLayout.LayoutParams srcRelativeParams_iv = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        
        srcRelativeParams_iv.addRule(RelativeLayout.CENTER_IN_PARENT,bgImageViewID);
        //RelativeParams_tv.addRule(RelativeLayout.ALIGN_TOP,bgImageViewID);
        rLayout.addView(src_iv, srcRelativeParams_iv);
        
        
        Button btn_ok = new Button(this);
        btn_ok.setId(buttonOkID);
        btn_ok.setBackgroundResource(R.drawable.submit);
        
        RelativeLayout.LayoutParams relativeParams_btn_ok = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        
        //RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)iv.getLayoutParams();
        
        relativeParams_btn_ok.addRule(RelativeLayout.ALIGN_BOTTOM,bgImageViewID);
        relativeParams_btn_ok.addRule(RelativeLayout.ALIGN_LEFT,bgImageViewID);
        rLayout.addView(btn_ok, relativeParams_btn_ok);
                
        Button btn_cancel = new Button(this);
        btn_cancel.setId(buttonCancelID);
        btn_cancel.setBackgroundResource(R.drawable.cancel);
            
        RelativeLayout.LayoutParams relativeParams_btn_cancel = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        relativeParams_btn_cancel.addRule(RelativeLayout.ALIGN_BOTTOM,bgImageViewID);
        relativeParams_btn_cancel.addRule(RelativeLayout.ALIGN_RIGHT,bgImageViewID);;
        rLayout.addView(btn_cancel, relativeParams_btn_cancel);
        
        //window.setContentView(R.layout.diag);
        window.setContentView(rLayout);
        // 为确认按钮添加事件,执行退出应用操作
        btn_ok.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                
            }
        });

        // 关闭alert对话框架
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dlg.cancel();
            }
        });
posted on 2014-06-26 16:00  千秋月  阅读(416)  评论(0编辑  收藏  举报