Correctly locate pop ups in Android

I insist on using pop-up positioning. When I click a button, a pop-up window is displayed. I hope it should be positioned according to the available space. In addition, if my button is in the center, it should be below the button. Here is my code. Please let me know what I'm wrong. Thank you

mBtnPopUp.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            ListView mListView = (ListView) mPopUpView.findViewById(R.id.pop_up_list_view);
            mListView.setAdapter(mPopuplistadapter);
            Drawable drawable = getResources().getDrawable(android.R.drawable.alert_light_frame);
            mPopupWindow.setBackgroundDrawable(drawable);
            // mPopupWindow.setBackgroundDrawable(new BitmapDrawable());

            showLikequickaction(0, 0);
            mPopupWindow.showAsDropDown(mBtnPopUp);
        }
    });

    mPopupWindow.setOutsideTouchable(true);
public void showLikequickaction(int xOffset, int yOffset)
{

    int[] location = new int[2];
    mBtnPopUp.getLocationOnScreen(location);

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

    mBtnPopUp.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

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

    @SuppressWarnings("deprecation")
    int screenWidth = mWindowManager.getDefaultDisplay().getWidth();


    int xPos = screenWidth - rootWidth + xOffset;
    int yPos = anchorRect.top - rootHeight + yOffset;

    if(rootWidth > anchorRect.right - anchorRect.left)
    {
        // right
        xPos = anchorRect.right - rootWidth;
    }
    else
    {
        // left
        xPos = anchorRect.left + 15;
    }

    if(xPos + rootWidth > screenWidth)
        xPos = screenWidth - rootWidth - 20;

    // display on bottom
    if(rootHeight > anchorRect.top)
    {
        yPos = anchorRect.bottom + yOffset;
        // mPopupWindow.setAnimationStyle(R.style.Animations_GrowFromTop);
    }

    mPopupWindow.showAtLocation(mBtnPopUp, Gravity.NO_GRAVITY, xPos, yPos);
}

thank you..

resolvent:

public void downloadBtnSelected(View anchor) {
    final ListPopupWindow lpw = new ListPopupWindow(this);
    String[] data = { ".png", ".pdf", ".jpg", ".jpeg" };
    PopupAdapter pa = new PopupAdapter(data, this);
    lpw.setAdapter(pa);

    //setting up an anchor view
    lpw.setAnchorView(anchor);

    //Setting measure specifications. I'v used this mesure specs to display my
    //ListView as wide as my anchor view is
    lpw.setHeight(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
    lpw.setWidth(anchor.getRight() - anchor.getLeft());

    // Background is needed. You can use your own drawable or make a 9patch.
    // I'v used a custom btn drawable. looks nice.
    lpw.setBackgroundDrawable(this.getResources().getDrawable(android.R.drawable.btn_default));

    // Offset between anchor view and popupWindow
    lpw.setVerticalOffset(3); 

    lpw.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Our action.....
            lpw.dismiss();

        }
    });
    lpw.show();

}

And a button with onclicklistener to call this method:

Button btn = new Button(this);
btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        downloadBtnSelected(v);
    }
});

This code allows you to display a pop-up window under BTN. The pop-up window width is the same as button 1. If you want to fill in the screen width - set width = linearlayout.match_ The parent pop-up menu will be displayed below the anchor view (if there are spaces), and if there are no spaces below, it will be exceeded

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>