好的,我终于弄清楚如何在复选框或其他视图旁边获得吐司
1.您需要通过以下方式获取视图屏幕上的位置
buttonView.getLocationOnScreen(location);
更多参考请参见获取屏幕位置(int[]) http://developer.android.com/reference/android/view/View.html#getLocationOnScreen%28int%5B%5D%29
2.使用设置重力来烘烤
toast.setGravity(Gravity.TOP|Gravity.LEFT,buttonView.getRight()+5,
location[1]-10);
这里重要的事情已经确定x-coordinate
视图右侧的吐司,
例如。,buttonView.getRight()
并从中获取 y 坐标location[1]
,你从中得到getLocationOnScreen()
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
Toast toast=Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
View view=toast.getView();
int location[]=new int[2];
buttonView.getLocationOnScreen(location);
switch(buttonView.getId())
{
case R.id.chkRed: if(chkRed.isChecked())
{
chkGreen.setChecked(false);
chkBlue.setChecked(false);
chkYellow.setChecked(false);
toast.setText("You Selected Red");
toast.setGravity(Gravity.TOP|Gravity.LEFT,buttonView.getRight()+5, location[1]-10);
view.setPadding(1, 1, 1, 1);
view.setBackgroundColor(Color.RED);
}
}
}