2014-06-25 51 views

回答

0

调整......下面

代码应该做的:

 View v = findViewById(R.id.youButtnView);      
     int location[]=new int[2]; 
     v.getLocationOnScreen(location); 
    Toast toast=Toast.makeText(getApplicationContext(), 
    "Your message", Toast.LENGTH_LONG); 
    toast.setGravity(Gravity.TOP|Gravity.LEFT,v.getRight()-25, location[1]-10); 
    toast.show(); 
5

1)获取该按钮的x坐标,你的按钮调用getLeft()。对于按钮底部的y坐标,请拨打getTop()getHeight()

2)将这些坐标放入吐司,使用setGravity(Gravity.TOP|Gravity.LEFT, x, y)

3)要使用户点击按钮时发生这种情况,请在按钮的onClick方法中执行此操作。

public void makeToast(View view){ 

    int x = view.getLeft(); 
    int y = view.getTop() + 2*view.getHeight(); 
    Toast toast = Toast.makeText(this, "see me", Toast.LENGTH_SHORT); 
    toast.setGravity(Gravity.TOP|Gravity.LEFT, x, y); 
    toast.show(); 
} 

(从逻辑上讲,我一直在想它应该是共达+的getHeight,但每次我试过了,面包上出现的按钮,而不是在其下方的顶部为2倍使其成为各种工作的高度)

而在你的xml:

<Button 
     <!-- put other attributes here --> 
     android:onClick="makeToast" />