我试过了
toast.setGravity(0,(int)v.getX(),(int)v.getY());
但这完全在整个地方创造了它.
如果重要,我的视图是tableRow中的一个元素.
谢谢
编辑:我不能使用PopupWindow执行此任务.
解决方法 我认为这 Tutorial将帮助您实现您想要的目标:public voID onClick(VIEw v) { int xOffset = 0; int yOffset = 0; Rect gvr = new Rect(); VIEw parent = (VIEw) v.getParent();// v is the image,//parent is the rectangle holding it. if (parent.getGlobalVisibleRect(gvr)) { Log.v("image left",Integer.toString(gvr.left)); Log.v("image right",Integer.toString(gvr.right)); Log.v("image top",Integer.toString(gvr.top)); Log.v("image bottom",Integer.toString(gvr.bottom)); VIEw root = v.getRootVIEw(); int halfwayWIDth = root.getRight() / 2; int halfwayHeight = root.getBottom() / 2; //get the horizontal center int parentCenterX = ((gvr.right - gvr.left) / 2) + gvr.left; //get the vertical center int parentCenterY = (gvr.bottom - gvr.top) / 2 + gvr.top; if (parentCenterY <= halfwayHeight) { yOffset = -(halfwayHeight - parentCenterY);//this image is above the center of gravity,i.e. the halfwayHeight } else { yOffset = parentCenterY - halfwayHeight; } if (parentCenterX < halfwayWIDth) { //this vIEw is left of center xOffset = -(halfwayWIDth - parentCenterX); } if (parentCenterX >= halfwayWIDth) { //this vIEw is right of center xOffset = parentCenterX - halfwayWIDth; } } Toast toast = Toast.makeText(activity,altText,Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER,xOffset,yOffset); toast.show(); }});总结
以上是内存溢出为你收集整理的Android – 创建一个锚定到视图的Toast全部内容,希望文章能够帮你解决Android – 创建一个锚定到视图的Toast所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)