如何将Toast的动态位置设置为视图? [英] How to set Dynamic position of Toast to a View?
问题描述
首先这不是完整的代码
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
Toast toast=Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
View view=toast.getView();
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.NO_GRAVITY,buttonView.getRight(), chkRed.getTop());
//top does not align
//it align perfectly to right of checkbox
view.setBackgroundColor(Color.RED);
}
break;
}
}
所以现在的问题是我想展示吐司在复选框旁边,我尝试使用 setGravity()
,但是它没有工作,已经被击中并尝试了很长时间,但是没有任何进展
So now the problem is that I want to display toast beside checkbox, I tried working with setGravity()
, but it just does not workup and have been hit and trying for long time, but with no progress
如何在复选框旁边显示吐司?
How to display toast beside the checkbox?
推荐答案
好的,我终于想出了如何在复选框或其他视图旁边举杯祝酒
ok, i finally figured out how to get toast beside checkbox or another view
1。。您需要在视图屏幕上找到位置,方法是
1. You need to get location on screen of the view, by
buttonView.getLocationOnScreen(location);
有关更多参考,请参见 getLocationOnScreen(int [])
for more Reference, see getLocationOnScreen(int[])
2。。使用
toast.setGravity(Gravity.TOP|Gravity.LEFT,buttonView.getRight()+5,
location[1]-10);
这里重要的是设置 x坐标
烤面包到视图的右侧,
,例如 buttonView.getRight()
,并从位置[1]获取y坐标
,您可以从 getLocationOnScreen()
the important thing here is set x-coordinate
of toast to right of view,
e.g., buttonView.getRight()
and get y-coordinate from the location[1]
,which you get from 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);
}
}
}
这篇关于如何将Toast的动态位置设置为视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!