码迷,mamicode.com
首页 > 其他好文 > 详细

纯代码实现对话框

时间:2014-06-27 14:45:14      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   ext   

对话框自定义:相对布局的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();
            }
        });

纯代码实现对话框,布布扣,bubuko.com

纯代码实现对话框

标签:style   class   blog   code   java   ext   

原文地址:http://www.cnblogs.com/qianqiuyue/p/3810255.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!