Android:通过代码在PopupWindow上设置边距

11

我希望在弹出窗口上设置左右边距。我尝试在layout上设置布局参数,然后设置边距,但没有效果。

有人可以给我设置弹出窗口边距的代码吗?

这是代码:

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

谢谢。

5个回答

22

由于您的布局是顶部对齐窗口,并且您正在使用屏幕的宽度和高度进行动态设置,因此要在所有边缘上设置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);

8
它只是从一侧减少宽度。 - Vahid

0

我的唯一解决方案是在custom_popup.xml布局中添加2个透明的相对布局,宽度和高度为-20dp(或其他)。只需将它们设置为主布局的右侧和顶部即可。

蓝图 屏幕

此外,您可以使用popup.showAsDropDown(anchorVIew,480,-100)手动分配位置,而不是使用showAtLocation()。


0

我也遇到了这个问题。经过大约两个小时的努力,我用以下方法解决了它。

private void setPopUpWindow(View v) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.dialog_menu_overflow, null);
    int width = ConstraintLayout.LayoutParams.WRAP_CONTENT;
    int height = ConstraintLayout.LayoutParams.WRAP_CONTENT;
    boolean focusable = true;
    popupWindow = new PopupWindow(view, width, height, focusable);
    popupWindow.showAsDropDown(v, 0, -customToolbar.getHeight());
}

在布局中

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:padding="@dimen/_6sdp"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="ContentDescription"
    android:background="@drawable/popup_menu_rounded_bg">

0

我知道我来晚了,但下面的代码对我起作用

popupWindow.apply {
            activity?.let {
                val xOffSet = TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP,
                    -20dp,                     it.resources.displayMetrics
                ).roundToInt()
                val yOffSet = TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP,
                    -10dp,
            it.resources.displayMetrics
                ).roundToInt()
                contentView = allRewardsOptionsMenuBinding.root
                showAsDropDown(anchorView, xOffSet, yOffSet)
            }
        }

-1
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(left, top, right, bottom) 的格式是这样的;

尝试这个:ratePw.update(int Left_Margin, int Top_Margin, int Width,int Height); 调整宽度以使其正常工作。 - Sachin D
1
我们无法在PopupWindow的布局参数中设置边距。这样行不通。我尝试了Jeet提供的上述解决方案,虽然需要进行一些调整,但它非常有效。 - Srujan Barai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接