android:通过代码在PopupWindow上设置margin

Jov*_*van 8 android

我想在popupwindow上设置左右边距.我尝试设置布局参数layout,然后设置保证金,但不起作用.

谁能给我代码在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);
Run Code Online (Sandbox Code Playgroud)

谢谢.

jee*_*eet 16

因为您的布局是窗口顶部,并且您通过使用屏幕的宽度和高度动态地执行此操作,因此要在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);
Run Code Online (Sandbox Code Playgroud)

  • 它只是从一侧减少宽度 (4认同)