如何让PopupWindow始终显示在按钮(视图)上方?

9

设计要求是,在列表视图中,每个条目里有一个按钮。按下该按钮后,弹出窗口始终显示在该按钮上方,而不是下方。

在Android中,使用“showAsDropDown”方法,Popupwindow默认显示在按钮(视图或锚点)的底部。 但如果底部不足以显示Popupwindow,则会显示在按钮(视图或锚点)的顶部。

onTop = (displayFrame.bottom - mScreenLocation[1] - anchor.getHeight() - yoff) <(mScreenLocation[1] - yoff - displayFrame.top);

所以,根据这一点,通过 "setSelectionFromTop" 移动按钮的项目,使判断不足以显示在按钮底部,以实现效果。

在 Android 4.0.3中,是可以的,项目会移动,弹出窗口会显示新位置和上方,但是,在Android 2.2中,弹出窗口仍然显示按下的位置,而非移动后的位置。

boolean onTop = (displayFrame.bottom - mScreenLocation[1] - v.getHeight() - 0) < (mScreenLocation[1] - 0 - displayFrame.top);
if(!onTop){
mListMain.setSelectionFromTop(mListMain.getPositionForView(v),(displayFrame.bottom - v.getHeight() + displayFrame.top) / 2 );
}

能帮助我,如何解决这个问题?!.. T_T
1个回答

6
也许情况不完全一样,但我的解决方案是:
public class BaloonView extends PopupWindow {   
    public BaloonView(Context context, View content) {
        super(context);
        setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        setTouchable(true);
        setFocusable(true);
        setOutsideTouchable(true);  
        setTouchInterceptor(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    BaloonView.this.dismiss();                
                  return true;
                }               
                return false;
              }
            });
    }

    public void showUnderView(View view, View content) {
        setBackgroundDrawable(view.getContext().getResources().getDrawable(R.drawable.popup_inline_error_holo_dark));
        FrameLayout container = new FrameLayout(view.getContext());
        container.addView(content);
        setContentView(container);
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        container.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        int xoffset = view.getWidth() / 2 - container.getMeasuredWidth() / 2;
        showAsDropDown(view, xoffset, 0);
    }
}

1
我们能否在锚点上方显示弹出窗口?目前的设置是在锚点下方显示。 - Prashanth Debbadwar

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