显示如何设置弹出窗口的位置?

3

我需要展示一个弹出框,虽然已经实现了,但是无法设置弹出框的位置。

我需要将弹出框设置在“朋友”标签下方。

代码:

_spinner = (Spinner) view.findViewById(R.id.group_spinner);
_groupAdaptor = new ArrayAdapter(getActivity(), 
                android.R.layout.simple_spinner_dropdown_item, _itemGroupList);
_spinner.setAdapter(_groupAdaptor);
_spinner.setOnItemSelectedListener(this);

当点击时,我会调用以下方法:

_spinner.performClick();

你能发布你的布局代码吗? - GrIsHu
你想使用自定义对话框还是下拉列表(spinner)? - Jitender Dev
3个回答

0
使用这段我的工作代码。
Rect r = locateView(v);    
final PopupWindow popup = new PopupWindow(getActivity());
            popup.setAnimationStyle(R.style.animation);
            popup.setContentView(layout);
            popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
            popup.setWidth(getActivity().getWindowManager().getDefaultDisplay().getWidth() / 2);
            popup.setFocusable(true);
            popup.setBackgroundDrawable(new BitmapDrawable());
            popup.showAtLocation(layout, Gravity.TOP | Gravity.LEFT, r.right, r.bottom);


public static Rect locateView(View v) {
        int[] loc_int = new int[2];
        if (v == null)
            return null;
        try {
            v.getLocationOnScreen(loc_int);
        } catch (NullPointerException npe) {
            return null;
        }
        Rect location = new Rect();
        location.left = loc_int[0];
        location.top = loc_int[1];
        location.right = loc_int[0] + v.getWidth();
        location.bottom = loc_int[1] + v.getHeight();
        return location;
    }

0

我通过一些布局更正解决了我的问题。我犯了一个傻瓜错误,Spinner视图没有正确对齐。

  <TextView
                android:id="@+id/group_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="@string/label_friends"
                android:textColor="@android:color/white" />

            <ImageView
                android:id="@+id/dropListImageview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/group_text"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="4dip"
                android:background="@drawable/droplist" />

            <Spinner
                android:id="@+id/group_spinner"
                android:layout_width="150dip"
                android:layout_height="20dip"
                android:layout_centerHorizontal="true"
                android:layout_below="@+id/group_text"
                android:visibility="invisible"
               />

这里的确切解决方案是什么? - IgorGanapolsky

-1

我在我的应用程序中也遇到了同样的问题,并通过使用spinier找到了解决方案。你所要做的就是在朋友的点击事件中使用spinier下拉列表,并在该菜单中显示你的选项。这将有所帮助。


2
为什么要复制粘贴user2119025的答案? - GrIsHu
标记此答案 @GrIsHu - Tofeeq Ahmad
1
其实这是我的错误,那是我自己创建的帐户,是一个错误,我将从该帐户中删除该答案。 - Amit Gupta

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