2013-05-02 53 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