2012-03-02 60 views
7

我想在popupwindow上設置左右邊距。我嘗試在layout上設置佈局參數,然後設置邊距,但不起作用。android:在PopupWindow上通過代碼設置邊距

誰能給我代碼,以便在popupwindow

設置保證金這裏是代碼

LayoutInflater inflater = (LayoutInflater) QuestionsActiviy.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View layout = inflater.inflate(R.layout.popup, (ViewGroup) findViewById(R.id.popup_element)); 


     ratePw = new PopupWindow(layout); 
     ratePw.setWidth(WindowManager.LayoutParams.FILL_PARENT); 
     ratePw.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); 
     ratePw.setFocusable(true); 

     ratePw.showAtLocation(layout, Gravity.CENTER, 0, 0); 

感謝。

回答

9

爲您的佈局是在窗口頂部,並且要動態這樣做,通過使用寬度和屏幕的高度,所以要設置SLL側緣10可以使用:

Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 
int width = size.x; 
int height = size.y; 

LayoutInflater inflater = (LayoutInflater) QuestionsActiviy.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View layout = inflater.inflate(R.layout.popup, (ViewGroup) findViewById(R.id.popup_element)); 


ratePw = new PopupWindow(layout); 
ratePw.setWidth(width-20); 
ratePw.setHeight(height-20); 
ratePw.setFocusable(true); 
0
LayoutParams parm1=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
parm1.setMargins(20, 20, 0, 0); 
layout.setLayoutParams(parm1); 
//ratePw.addView(layout,parm1); // removed it 
ratePw.setContentView(layout); 

其實,layoutParams.setMargins的格式(左,上,右,下);

+0

ratePw不支持addView – Jovan 2012-03-02 12:37:11

+0

試試這個:'RatePw.update(int Left_Margin,int Top_Margin,int Width,int Height);'調整寬度以使其可以工作 – 2012-03-04 05:56:22

+0

我們無法給popupwindow佈局參數。這不工作。我嘗試了jeet的上述解決方案,並且像魅力一樣工作(雖然只有很少的調整)。 – 2015-11-12 19:31:24

相關問題