2013-05-02 66 views
1

我的UI有2個LinearLayout,layout1和layout2。
並使用下面的代碼顯示對話框:在Android上的佈局中顯示對話框

private AlertDialog MyDialog; 
public void adddialog() { 
    AlertDialog.Builder MyBuilder = new AlertDialog.Builder(this); 
    MyBuilder.setTitle("Title") 
    MyBuilder .setView(ListView); 
    MyDialog = MyBuilder.create(); 
} 

我想設置佈局2的右上角的對話框位置。
我該如何抵達?

回答

2
private AlertDialog MyDialog; 
public void adddialog() 
{ 
    AlertDialog.Builder MyBuilder = new AlertDialog.Builder(this); 
    MyBuilder.setTitle("Title") 
    MyBuilder .setView(ListView); 
    MyDialog = MyBuilder.create(); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    WindowManager.LayoutParams dialogParam = MyDialog.getWindow().getAttributes(); 

dialogParam.gravity = Gravity.TOP | Gravity.RIGHT; 
// dialogParam.x = 100; //x position 
// dialogParam.y = 100; //y position 

MyDialog.show(); 
} 
+0

但是這種方法顯示在屏幕右上角,而不是佈局的。佈局的位置可能不固定。 – brian 2013-05-02 10:32:36

+0

@ brian-你是否在動態創建佈局?如果你的佈局位置是固定的,那麼你可以通過使用x和y位置來設置對話框。 – TheFlash 2013-05-02 10:35:07

+0

但是,因爲您已經發布 - 佈局的位置可能不固定。所以我也想知道如何設置對話框。 – TheFlash 2013-05-02 10:36:52