2016-03-28 113 views
0

我有個親戚佈局R和一個alertdialog答:我想設置相同,R A的位置和尺寸這樣的位置和尺寸上:下面的一組代碼的Android alertdialog設置佈局

A.position_x = R.get_position_x() 
A.position_y = R.get_position_y() 
A.width = R.get_width() 
A.height = R.get_height() 
+0

代替position_x離開position_y使用和頂部 – arun

+0

你有GET PARAM並設置PARAM使用該 –

回答

0

使用AlertDialog在屏幕的上方:

    AlertDialog.Builder builder = new AlertDialog.Builder(
          MainActivity.this); 
        builder.setTitle("Title:"); 
        builder.setMessage("Are you sure to Exit?"); 

        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          //Set ypu positive code on OK button 
          finish(); 
         } 
        }); 
        // Setting Negative "NO" Btn 
        builder.setNegativeButton("NO", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int which) { 
            dialog.cancel(); 
           } 
          }); 
        // Showing Alert Dialog 
        AlertDialog dialog = builder.create(); 
        dialog.getWindow().setGravity(Gravity.TOP); 
        dialog.show(); 
+0

它不工作BEC我希望將對話框設置爲相對佈局,並將其寬度和高度設置爲相對佈局。 –

+0

好的,你可以在你的問題中發佈你的對話框代碼和相對佈局的xml代碼。 –

0
Get height,width and position x and y of relativelayout from below code 

    rel1 = (RelativeLayout)findViewById(R.id.rel1); 

     rel1.post(new Runnable() 
     { 

      @Override 
      public void run() 
      { 
       widthRel1 = rel1.getWidth(); 
       heightRel1 = rel1.getHeight(); 

       xRel1 = (int) rel1.getX(); 
       yRel1 = (int) rel1.getY(); 

      } 
     }); 

Then apply it to Dialog 



final Dialog alertDialog = new Dialog(MainActivity.this); 
      alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      alertDialog.setContentView(R.layout.template_menu); 

      Window window = alertDialog.getWindow(); 
      WindowManager.LayoutParams wlp = window.getAttributes(); 


       wlp.x = xRel1; //x position   
       wlp.y = yRel1; //y position 

       wlp.height = heightRel1; 
      wlp.width = widthRel1; 

     wlp.gravity = Gravity.TOP | Gravity. LEFT; 


      window.setAttributes(wlp); 
      window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); 

      alertDialog.show();