2013-05-02 31 views
0

我想在我的應用程序中有一個彈出對話框。該對話最好作爲一項活動完成。然後我使用我的清單中的「Theme.Dialog」使活動表現爲對話框。這工作正常。Android:作爲對話框的活動位置

似乎有什麼不工作的定位,我想它的彈出窗口。

這裏是彈出的模板,這應該在圓形 按鈕爲中心的圓形菜單。只是在「甜甜圈」周圍佈置了六個圖標的相對佈局 enter image description here

我想要做的是在屏幕上通過幾個圓形按鈕之一顯示彈出窗口。根據按下哪個按鈕,我收集按下的View(按鈕)的X/Y位置,並通過綁定將其發送到活動。然後,我想要使用按下按鈕的位置和彈出窗口的大小來定位對話框,以便將它居中放置在按鈕上方。

public class WirelessDialog extends Activity { 



@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    //requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.donut_dialogue); 

    // get the layout of the dialog 
    RelativeLayout rl = (RelativeLayout)findViewById(R.id.donut_layout); 

    // get base image all other icons reside on and compute its haLf-width/height 
    ImageView iv = (ImageView)findViewById(R.id.donut); 
    int half_width = iv.getWidth()/2 ; 
    int half_height = iv.getHeight()/2; 


    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
     int startX = extras.getInt("X"); 
     int startY = extras.getInt("Y"); 
     int endX = extras.getInt("XE"); 
     int endY = extras.getInt("YE"); 

     WindowManager.LayoutParams winParms = getWindow().getAttributes() ; 
     int x = winParms.x; 
     int y = winParms.y; 
     Log.d("DIALOG", "Original x/y: " + x + " " + y); 
     //int width = winParms.width; 
     //int height = winParms.height; 

     int xc = startX + (endX - startX + 1)/2; 
     int yc = startY + (endY - startY + 1)/2; 


     // new location of the dialog should be 1/2 the width and height of the donut subtracted 
     // from the center of the button that was pressed. 

     winParms.x = (xc - half_width); 
     winParms.y = (yc - half_height); 
     Log.d("DIALOG", "New x/y: " + winParms.x + " " + winParms.y); 
     getWindow().setAttributes(winParms); 
    } 

} 

設置佈局參數只強制對話框到最右邊。最初,這是 x/y位置返回爲零,但任何非零數字甚至不會將其放置在位置指示的位置。

將其放置在0,0是屏幕的中心,並且設置X/Y位置爲負力它到uppper左側。數學不是我所期望的基於文檔

任何人都有一個想法,我應該怎麼做才能正確放置此對話窗口? 一個典型的例子是在1280x800屏幕平板電腦上的(400,470)按鈕。

回答

0

我會做什麼在這種情況下:

創建一個自定義視圖類是圓形彈出; 當然,如果選項永遠都是相同的,則在您的活動初始化中實例化此視圖; On button單擊偵聽器,獲取其X,Y,寬度和高度,您的X和Y位置將爲,buttonX +(buttonWidth/2),buttonY +(buttonHeight/2); 添加此視圖並將其放置在此X和Y位置上。爲了更快速和「更便宜」,您的視圖可以在創建活動時被實例化,並且在視圖佈局根元素中是不可見的(android:visible =「none」)。

我認爲你的Activity根佈局必須是一個RelativeLayout或FrameLayout。

我認爲這個解決方案應該可以工作。

+0

是的,自定義視圖類可能會工作,但應該使用活動作爲對話框。 – Martin 2013-05-03 16:10:16