2012-03-02 1033 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

相关问题