2012-03-02 6 views
7

Quiero establecer los márgenes izquierdo y derecho en popupwindow. Intento configurar parámetros de diseño en layout, y luego establezco el margen, pero no trabajo.android: establecer el margen en PopupWindow por el código

Puede alguien darme el código para establecer el margen de popupWindow

Aquí está el código

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); 

Gracias.

Respuesta

9

como su diseño es la parte superior de la ventana, y que están haciendo esto de forma dinámica, mediante el uso de la anchura y la altura de la pantalla, por lo que para establecer el margen de 10 en el lado SLL puede utilizar:

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); 

realidad , el formato de layoutParams.setMargins (izquierda, arriba, derecha, abajo);

+0

ratePw no compatible addView – Jovan

+0

Pruebe esto: 'ratePw.update (int Left_Margin, int Top_Margin, int Ancho, int Height);' ajuste el ancho para que funcione –

+0

No podemos asignar márgenes a los params de diseño en popupwindow. Esto no es trabajo Probé la solución anterior de jeet y funciona como un amuleto (aunque con algunos ajustes). –

Cuestiones relacionadas