设置敬酒动态位置视图 [英] Set Dynamic position of Toast to a View

查看:149
本文介绍了设置敬酒动态位置视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这是不完全的code

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

所以,现在的问题是,我想显示旁边的复选框敬酒,我尝试正在与 setGravity(),但它只是不经处理并已打了努力了很长一段时间,但没有取得进展。

So Now, the problem is, 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);

有关更多参考,请参阅<一href="http://developer.android.com/reference/android/view/View.html#getLocationOnScreen%28int%5B%5D%29">getLocationOnScreen(int[])

2。使用:设置重力来敬酒

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

这里最重要的设置 x坐标土司的观点正确,   例如, buttonView.getRight(),并从位置的y坐标[1] ,你从中获取 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);
            }

        }
    }

这篇关于设置敬酒动态位置视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆