Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android – 创建一个锚定到视图的Toast大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想创建一个锚定到View的Toast(即,在给定的View下面显示).

我试过了

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所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。