在android中显示自定义快速操作弹出窗口

时间:2013-02-04 08:25:52

标签: android popupwindow

我正在尝试在我的项目中构建快速Action。当我点击视图时,它会正确显示弹出窗口。但是没有显示指向父视图的箭头(向上箭头,向下箭头)。我在很多方面尝试过它。如果有人有更好的解决方案或良好的逻辑,请帮助我 谢谢:  在这里你可以看到代码:

public void show (View anchor) {
        preShow();

        int[] location      = new int[2];

        anchor.getLocationOnScreen(location);

        Rect anchorRect     = new Rect(location[0], location[1], location[0] + anchor.getWidth(), location[1] 
                            + anchor.getHeight());

        mRootView.setLayoutParams(new LayoutParams(300, 400));
        mRootView.measure(200,300);

        int rootWidth       = mRootView.getMeasuredWidth();
        int rootHeight      = mRootView.getMeasuredHeight();

        int screenWidth     = mWindowManager.getDefaultDisplay().getWidth();

        int xPos            = (screenWidth - rootWidth) / 2;
        int yPos            = anchorRect.top - rootHeight;

        boolean onTop       = true;

        if (rootHeight > anchor.getTop()) {
            yPos    = anchorRect.bottom;
            onTop   = false;
        }
        if(onTop==true){
            showArrow(R.id.arrow_down, anchorRect.centerX());
        }
        else
        {
            showArrow( R.id.arrow_up, anchorRect.centerX());
        }

和showArrow方法如下:

private void showArrow(int whichArrow, int requestedX) {
        final View showArrow;
        final View hideArrow;
        if(whichArrow==R.id.arrow_down)
        {
            showArrow=mArrowDown;
            hideArrow=mArrowUp;

        }
        else{
            showArrow=mArrowUp;
            hideArrow=mArrowDown;       
        }


        final int arrowWidth = mArrowUp.getMeasuredWidth();

        showArrow.setVisibility(View.VISIBLE);


        ViewGroup.MarginLayoutParams param = (ViewGroup.MarginLayoutParams)showArrow.getLayoutParams();

        param.leftMargin = requestedX - arrowWidth / 2;

        hideArrow.setVisibility(View.INVISIBLE);
    }

1 个答案:

答案 0 :(得分:0)

你的代码没有任何问题。可能,你的xml文件中有些东西会出错。请检查您的xml文件。尝试修改你的xml文件。希望你的问题能得到解决。