純代碼實現佈局,對話框

對話框自定義:相對佈局的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("XXXXXXXXX");

  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();
   }
  });

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章