6

我坚持使用弹出窗口定位。我在单击按钮时显示我的弹出窗口。我希望它应该根据可用空间定位。此外,如果我的按钮在中心,它应该在按钮下方。下面是我的代码。请让我知道我错在哪里。谢谢你。

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);
}

谢谢..

4

4 回答 4

4

我知道它很晚才安静,但我希望它会对某人有所帮助。

LayoutInflater layoutInflater = getLayoutInflater();                
int popupWidth = 500;//ViewGroup.LayoutParams.WRAP_CONTENT;
int popupHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
popupView = layoutInflater.inflate(R.layout.main, null);

PopupWindow attachmentPopup = new PopupWindow(this);
attachmentPopup.setFocusable(true);
attachmentPopup.setWidth(popupWidth);
attachmentPopup.setHeight(popupHeight);         
attachmentPopup.setContentView(popupView);
attachmentPopup.setBackgroundDrawable(new BitmapDrawable());
attachmentPopup.showAsDropDown(v, -5, 0);

它将根据按钮的位置定位您的弹出窗口。

于 2015-04-16T11:42:18.463 回答
3
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();

}

以及带有 onClickListener 的按钮来调用此方法:

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

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

此代码让您在 btn 下方显示一个弹出窗口,弹出窗口的宽度将与按钮宽度相同。如果要填充屏幕宽度- set width=LinearLayout.MATCH_PARENT 弹出菜单将显示在锚视图下方,如果有空间,如果下方没有空间,则超出

于 2013-04-29T06:16:53.617 回答
1

此代码将在您特定视图的中心显示完美的弹出窗口

// click event of specific view
  @Override
    public void onClick(View v) {
        super.onClick(v);
       if (v == lin_filterActionbar) {
            PopupWindow popupwindow_obj; // create object
            popupwindow_obj = dialog_AllergiesSelect(RR_HomeActivity.this,lin_filterActionbar);
            popupwindow_obj.showAsDropDown(lin_filterActionbar);

        } 
    }



 private PopupWindow dialog_AllergiesSelect(Context context,LinearLayout lin) {

        final PopupWindow dialog_AllergiesSelect = new PopupWindow(this);

        View v = View.inflate(context, R.layout.dialog_filter_actionbar, null);
        dialog_AllergiesSelect.setContentView(v);
        dialog_AllergiesSelect.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        dialog_AllergiesSelect.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        dialog_AllergiesSelect.setFocusable(true);

//        TextView lis_testSelection = (TextView) v.findViewById(R.id.dialogfilter_txt_filter);
//        LinearLayout.LayoutParams lin_laLayoutParams = new LinearLayout.LayoutParams((int) (sunApplication.getDeviceWidth() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_spinner_width)), (int) (sunApplication.getDeviceHeight() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_height)));
//        lis_testSelection.setLayoutParams(lin_laLayoutParams);

        // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
        int OFFSET_X = 0;
        int OFFSET_Y = 0;

        // Clear the default translucent background
        dialog_AllergiesSelect.setBackgroundDrawable(new BitmapDrawable());

        // Displaying the popup at the specified location, + offsets.
        //dialog_AllergiesSelect.showAtLocation(v, Gravity.C, p.x + OFFSET_X, p.y + OFFSET_Y);

        Rect location = locateView(lin);

        dialog_AllergiesSelect.showAtLocation(v, Gravity.TOP|Gravity.CENTER, 0, location.bottom);

        // Getting a reference to Close button, and close the popup when clicked.

        return dialog_AllergiesSelect;

    }
    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)
        {
            //Happens when the view doesn't exist on screen anymore.
            return null;
        }
        Rect location = new Rect();
        location.left = loc_int[0];
        location.top = loc_int[1];
        location.right = location.left + v.getWidth();
        location.bottom = location.top + v.getHeight();
        return location;
    }
于 2017-02-22T13:36:28.593 回答
0

对于自定义弹出窗口,制作一个名为的 XML 文件

cust_toast_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="45dp" >

    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        android:textStyle="bold"
        android:gravity="center"
        android:paddingTop="200dp"/>

</LinearLayout>

然后在你想要这个弹出窗口的java文件中添加下面的代码

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.cust_toast_layout,
                               (ViewGroup)findViewById(R.id.toast_layout_root));



TextView text = (TextView) layout.findViewById(R.id.text);
//text.setText("Excellent !!!");
text.setBackgroundResource(R.drawable.right_ans);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();

在 buttonclick 上,您可以调用一种方法,并在该方法中包含此代码。您可以通过编辑 xml 文件根据您的要求进行更改。

于 2013-04-29T06:21:19.080 回答