PopupWindow .showAsDropDown() 无法向左或向右移动(但上下都可以)

PopupWindow .showAsDropDown() unable to shift left or right (But up and down both work)

我已经使用 .showAsDropDown() 方法膨胀了 PopupWindow 但是我不确定为什么它不允许我向右或向左移动它。上下移动时效果很好

public class TestWindow extends PopupWindow {

    private final Context context;

    public TestWindow (Context context) {
        super(context);
        this.context = context;

        setupView();
    }

    private void setupView() {
        View view = LayoutInflater.from(context)
                .inflate(R.layout.popup_window_wallet_options, null);
        ButterKnife.bind(this, view);

        setOutsideTouchable(true);
        setFocusable(true);
        setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.bgr_menu_clear_wallet));
        setElevation(SpacingUtils.convertIntToDP(context, 4));

        setContentView(view);
    }
}
PopupWindow popupWindow = new TestWindow(context);
popupWindow.showAsDropDown(anchorButton, 50, -30);

将菜单向上移动 30 非常好,但我也试图将其向左移动,但它不起作用。我做错了什么?

注:

我已经用 50-50 试过了所以我不知道为什么它没有水平移动

我的R.layout.popup_window_wallet_options

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/bgr_menu_clear_wallet">

    <TextView
        android:id="@+id/tv_wallet_qr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foreground="?attr/selectableItemBackground"
        android:padding="6dp"
        android:text="Wallet QR"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/tv_clear_wallet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foreground="?attr/selectableItemBackground"
        android:padding="6dp"
        android:text="Clear wallet"
        android:textColor="@color/black" />

</LinearLayout>

Disclaimer: This is not a direct fix to showAsDropDown(), but it could be a workaround with showAtLocation().

通过使用 window decorView 作为 anchorView,并将实际的 anchorView 位置累加到 x 和 y 偏移值。

int[] anchorView = new int[2];
anchorButton.getLocationInWindow(anchorView); // anchor button location in the window
popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.NO_GRAVITY,
        anchorView[0] + 50,
        anchorView[1] + anchorButton.getHeight() -30);

更新:

The anchorButton is an ImageView. Can you post your code + xml for it

没什么特别的。 这是弹出 window 布局

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test clear"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity只有一个按钮作为anchorView:

并调用它:

Button button = findViewById(R.id.button);
PopupWindow popupWindow = new TestWindow(MainActivity.this);
popupWindow.showAsDropDown(button, 50, -30);