2012-03-02 37 views
7

Tôi muốn đặt lề trái và phải trên popupwindow. Tôi thử đặt thông số bố cục trên layout và sau đó đặt lề, nhưng không hoạt động.android: đặt lề trên PopupWindow theo mã

bất cứ ai có thể cho tôi mã cho thiết lập lề trên popupwindow

Đây là mã

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

Cảm ơn.

Trả lời

9

như bố trí của bạn là hàng đầu trên cửa sổ, và bạn đang làm điều này tự động, bằng cách sử dụng chiều rộng và chiều cao của màn hình, vì vậy để thiết lập lề 10 về phía sll bạn có thể sử dụng:

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

Trên thực tế , định dạng layoutParams.setMargins (trái, trên, phải, dưới);

+0

ratePw không hỗ trợ addView – Jovan

+0

Hãy thử điều này: 'ratePw.update (int Left_Margin, int Top_Margin, int Width, int Height); 'điều chỉnh chiều rộng để nó hoạt động –

+0

Chúng tôi không thể cung cấp lề cho các tham số bố cục trong popupwindow. Công việc này aint. Tôi đã thử các giải pháp trên từ jeet và làm việc của nó như một say mê (với vài điều chỉnh mặc dù). –

Các vấn đề liên quan