【问题标题】:android popup window showAtLocation not workingandroid弹出窗口showAtLocation不起作用
【发布时间】:2019-01-14 07:45:53
【问题描述】:

我试图在回收站视图中的特定视图上显示一个弹出窗口,但它没有显示在该位置。

这是我为显示透明弹出窗口所做的:

private void showPopup(View view) {

    LayoutInflater inflater = (LayoutInflater) HomeActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View layout = inflater.inflate(R.layout.fadepopup, findViewById(R.id.fadePopup));
    PopupWindow fadePopup = new PopupWindow(layout, 300, 500, true);
    fadePopup.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, android.R.color.transparent)));
    fadePopup.setOutsideTouchable(true);

    fadePopup.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER, 0, 0);
}

我从回收站视图中调用它,例如:

horizontalRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
            @Override
            public void onClick(View view, int position) {

                String menuTitle = menuTitles[position % menuTitles.length];
                Fragment selectedFragment = null;

                switch (menuTitle) {

                    case "Messenger":
                        showPopup(view);
                        break;

                }

            }

            @Override
            public void onLongClick(View view, int position) {

            }
        }));

另外,我尝试了showAsDropDown,但没有工作。

结果是:

更新:

XML 代码:

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

    <ImageView
        android:id="@+id/messenger_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/circular_white"
        android:padding="10dp"
        android:src="@drawable/nav_messanger" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/circular_white"
        android:padding="10dp"
        android:src="@drawable/nav_contacts" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/circular_white"
        android:padding="10dp"
        android:src="@drawable/nav_boss" />

</LinearLayout>

【问题讨论】:

    标签: android android-popupwindow


    【解决方案1】:

    在我看来,您应该尝试[相对布局][1]。 当您有一个填充整个屏幕的相对布局时,当您将 android:layout_alignParentBottom 添加到 XML 并将 Popup 作为元素添加到使用 setVisibility 在 fadePopup 上显示的 XML 时,Popup 应该显示在屏幕底部:使用 setVisibility:Invisible 可见和隐藏。所以你基本上可以为你传递给fadePopup方法的弹出窗口添加一个自定义布局,参数为setVisibility:visible。

    【讨论】:

    • 好的,我会添加它,但这应该是一个评论。
    • @ShylendraMadda:是的,你是对的。很抱歉。
    【解决方案2】:

    这是showAtLocation()PopupWindow.java中的实现:

    /**
     * <p>
     * Display the content view in a popup window at the specified location. If the popup window
     * cannot fit on screen, it will be clipped. See {@link android.view.WindowManager.LayoutParams}
     * for more information on how gravity and the x and y parameters are related. Specifying
     * a gravity of {@link android.view.Gravity#NO_GRAVITY} is similar to specifying
     * <code>Gravity.LEFT | Gravity.TOP</code>.
     * </p>
     *
     * @param parent a parent view to get the {@link android.view.View#getWindowToken()} token from
     * @param gravity the gravity which controls the placement of the popup window
     * @param x the popup's x location offset
     * @param y the popup's y location offset
     */
    public void showAtLocation(View parent, int gravity, int x, int y) {
        mParentRootView = new WeakReference<>(parent.getRootView());
        showAtLocation(parent.getWindowToken(), gravity, x, y);
    }
    

    如您所见,参数parent 是显示弹出窗口的根视图,类似于活动的布局。
    但是你为这个参数传递了你在弹出窗口中膨胀的布局!
    编辑
    您必须传递希望弹出窗口锚定的视图,x, y 将是其位置的偏移量。

    【讨论】:

    • 好的,感谢您的回复,我会尽力回复您。
    • 我试图通过参数parent 传递实际视图,但它并没有准确地显示在那个位置,而是直接显示在它上面。
    • 实际上,底部导航按钮在回收站视图中,所以我通过了该选定位置的视图,但仍然无法在该位置显示。
    • 我更新了我的问题,添加了如何从回收站视图中调用它
    猜你喜欢
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 2018-12-17
    • 2014-12-21
    • 2016-10-03
    相关资源
    最近更新 更多