【问题标题】:PopupWindow .showAsDropDown() unable to shift left or right (But up and down both work)PopupWindow .showAsDropDown() 无法向左或向右移动(但上下都可以)
【发布时间】:2021-12-26 22:39:18
【问题描述】:

我使用.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>

【问题讨论】:

  • 请忽略... 中的任何特殊属性。可能是什么原因造成的
  • 我在上面的编辑中添加了我在... 中的内容。你试过我的弹出窗口吗?它对你有用吗?
  • 我无法重现此问题;但希望showAtLocation() 可以成为您的解决方法
  • .showAsDropdown() 为您工作? (让你左右移动?)
  • 是的,工作正常。。你用的是普通按钮吗?还是选项菜单按钮?

标签: android popupwindow


【解决方案1】:

免责声明:这不是对showAsDropDown() 的直接修复,但它可能是showAtLocation() 的解决方法。

将窗口decorView作为anchorView,将anchorView的实际位置累加为x & y shift值。

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

更新:

anchorButton 是一个 ImageView。你能发布你的代码+xml吗

没有什么比你更特别的了。 这是弹窗布局

<?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>

活动只有一个按钮作为anchorView:

并称它为:

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

【讨论】:

  • 我不知道为什么,但是 -50dp 没有做任何改变它。使用 -75dp、-100dp 弹出窗口最终向左移动。我的 popup_window_layout 没有任何负边距(检查 layout 的编辑)所以我不确定为什么它没有向左移动 -50。
  • 可能它在某些 API 级别上不起作用,我看到了类似的东西;您可以在不同的 API 上对其进行测试
  • 不确定。我在我的 API27 模拟器和真实设备 30、31 上尝试过。同样的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多