正确定位Android中的d出窗口

正确定位Android中的d出窗口,第1张

概述我坚持使用d出窗口定位.我点击按钮时显示d出窗口.我希望它应根据可用空间定位.此外,如果我的按钮位于中心,它应该在按钮下方.下面是我的代码.请让我知道我哪里错了.谢谢.mBtnPopUp.setOnClickListener(newOnClickListener(){@OverridepublicvoidonC

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

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

谢谢..

解决方法:

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下面显示一个d出窗口
d出窗口宽度与按钮1相同.
如果你想填充屏幕宽度 – 设置宽度= linearLayout.MATCH_PARENT
d出菜单将显示在锚点视图下方(如果有空格),如果下方没有空格则超出

总结

以上是内存溢出为你收集整理的正确定位Android中的d出窗口全部内容,希望文章能够帮你解决正确定位Android中的d出窗口所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1118979.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存