How to set Dynamic position of Toast to a View?

南笙酒味 提交于 2019-12-05 21:39:38

问题


First of all this is not complete code

@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;
}
}

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?


回答1:


ok, i finally figured out how to get toast beside checkbox or another view

1. You need to get location on screen of the view, by

buttonView.getLocationOnScreen(location);

for more Reference, see getLocationOnScreen(int[])

2. Set gravity to toast using

toast.setGravity(Gravity.TOP|Gravity.LEFT,buttonView.getRight()+5, 
location[1]-10);

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);
            }

        }
    }


来源:https://stackoverflow.com/questions/14460269/how-to-set-dynamic-position-of-toast-to-a-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!