2010-07-01 399 views
4

我想在狀態欄後面設置AlertDialog位置,當我的對話框中的內容增加時,該怎麼做?我創建一個自定義AlertDialog用我自己的佈局....請幫我....如何在Android中將AlertDialog的位置設置爲頂部的屏幕?


下面是我的代碼,我設置的高度和alertDialog的xy位置,但仍亙古不變的節目其效果..

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     LayoutInflater inf = getLayoutInflater(); 
     View layout = inf.inflate(R.layout.main, null); 
     builder.setView(layout); 
     builder.setTitle("Add to Home screen"); 
    AlertDialog dialog = builder.create(); 
     WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes(); 
     int dialogOriginalHeight = WMLP.height; 
    WMLP.height += 750; 
    Log.i("XnY", "x="+WMLP.x+", y="+WMLP.y); 
    WMLP.x = -10; //x position 
    WMLP.y = -10; //y position 
    Log.i("XnY", "x="+WMLP.x+", y="+WMLP.y); 
    dialog.getWindow().setAttributes(WMLP); 
    Log.i("POSITION", "POS::HEIGHT:"+WMLP.height); 
    dialog.show(); 
+0

我覺得你有做.setView()..你能告訴你的代碼呢? – 2010-07-01 23:43:45

+0

下面是我的代碼,我設置了alertDialog的高度和x-y位置,但仍然沒有顯示它的效果.... – KISH 2010-07-02 07:32:57

+0

AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inf = getLayoutInflater(); View layout = inf.inflate(R.layout.main,null); builder.setView(layout); \t AlertDialog dialog = builder.create(); WindowManager.LayoutParams WMLP = dialog.getWindow()。getAttributes(); WMLP.height + = 750; (「XnY」,「x =」+ WMLP.x +「,y =」+ WMLP.y); \t WMLP.x = -10; \t // x位置 \t WMLP.y = -10; \t // y位置 \t Log.i(「XnY」,「x =」+ WMLP.x +「,y =」+ WMLP.y); \t dialog.getWindow()。setAttributes(WMLP); \t Log.i(「POSITION」,「POS :: HEIGHT:」+ WMLP.height); dialog.show(); – KISH 2010-07-02 07:34:38

回答

1

我知道這是真的老了,但對任何人,這後來看到:

x和y,因爲使用的是默認的重力被忽略。

docs

此窗口X位置。使用默認的重力會被忽略。當使用LEFT或START或RIGHT或END時,它會提供給定邊的偏移量。

我需要我從右上角偏移,所以我將我的引力設置爲0x00000035。這是最重要的。 (頂部是0x00000030,右邊是0x00000005)。這將導致x和y不被忽略。

alert.getWindow().setGravity(0x00000035); 
+0

這是真的,但如果你嘗試,它不會解決原來的問題。 x和y的負值似乎不起作用。 – doplumi 2014-03-01 20:38:07

1
WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes(); 

wmlp.gravity = Gravity.BOTTOM | Gravity.RIGHT; 
wmlp.x = 50; //x position 
wmlp.y = 50; //y position 

dialog.show(); 

可以是一個很好的選擇

相關問題