如何在Android中更改Toast的位置?

如何在Android中更改Toast的位置?,android,toast,Android,Toast,当我使用Toast在屏幕上显示一些弹出文本时,它会在屏幕底部稍上方显示文本,这是默认位置 现在我想把它显示在屏幕中间或者根据我的选择。 有谁能指导我如何做到这一点吗?来自 定位您的祝酒词 屏幕底部会出现标准的toast通知, 水平居中。您可以使用 setGravity(int,int,int)方法。它接受三个参数:a 重力常数、x位置偏移和y位置偏移 例如,如果您决定祝酒词应出现在 左上角,可以按如下方式设置重力: toast.setGravity(Gravity.TOP|Gravity.LEF

当我使用
Toast
在屏幕上显示一些弹出文本时,它会在屏幕底部稍上方显示文本,这是默认位置

现在我想把它显示在屏幕中间或者根据我的选择。 有谁能指导我如何做到这一点吗?

来自

定位您的祝酒词

屏幕底部会出现标准的toast通知, 水平居中。您可以使用
setGravity(int,int,int)
方法。它接受三个参数:a
重力
常数、x位置偏移和y位置偏移

例如,如果您决定祝酒词应出现在 左上角,可以按如下方式设置重力:

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
// v is the Button view that you want the Toast to appear above 
// and messageId is the id of your string resource for the message

private void displayToastAboveButton(View v, int messageId)
{
    int xOffset = 0;
    int yOffset = 0;
    Rect gvr = new Rect();

    View parent = (View) v.getParent(); 
    int parentHeight = parent.getHeight();

    if (v.getGlobalVisibleRect(gvr)) 
    {       
        View root = v.getRootView();

        int halfWidth = root.getRight() / 2;
        int halfHeight = root.getBottom() / 2;

        int parentCenterX = ((gvr.right - gvr.left) / 2) + gvr.left;

        int parentCenterY = ((gvr.bottom - gvr.top) / 2) + gvr.top;

        if (parentCenterY <= halfHeight) 
        {
            yOffset = -(halfHeight - parentCenterY) - parentHeight;
        }
        else 
        {
            yOffset = (parentCenterY - halfHeight) - parentHeight;
        }

        if (parentCenterX < halfWidth) 
        {         
            xOffset = -(halfWidth - parentCenterX);     
        }   

        if (parentCenterX >= halfWidth) 
        {
            xOffset = parentCenterX - halfWidth;
        }  
    }

    Toast toast = Toast.makeText(getActivity(), messageId, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, xOffset, yOffset);
    toast.show();       
}
如果要向右移动该位置,请增加 第二个参数。若要向下移动,请增加最后一个的值 参数


如果出现错误,指示必须调用makeText,则以下代码将修复该错误:

Toast toast= Toast.makeText(getApplicationContext(), 
"Your string here", Toast.LENGTH_SHORT);  
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();

以上代码将帮助您在屏幕中间显示烤面包或根据您的选择,只需根据您的需要设置“敬酒重力”< /P>


注意:在这个过程中,您必须使用Toast对象,您可以使用以下命令自定义Toast的位置:

setGravity(intgravity,intxoffset,intyoffset)

这样你就可以非常明确地知道你想把吐司放在哪里

关于xOffset和yOffset参数最有用的一点是,可以使用它们相对于某个视图放置Toast

例如,如果要制作显示在按钮顶部的自定义Toast,可以创建如下函数:

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
// v is the Button view that you want the Toast to appear above 
// and messageId is the id of your string resource for the message

private void displayToastAboveButton(View v, int messageId)
{
    int xOffset = 0;
    int yOffset = 0;
    Rect gvr = new Rect();

    View parent = (View) v.getParent(); 
    int parentHeight = parent.getHeight();

    if (v.getGlobalVisibleRect(gvr)) 
    {       
        View root = v.getRootView();

        int halfWidth = root.getRight() / 2;
        int halfHeight = root.getBottom() / 2;

        int parentCenterX = ((gvr.right - gvr.left) / 2) + gvr.left;

        int parentCenterY = ((gvr.bottom - gvr.top) / 2) + gvr.top;

        if (parentCenterY <= halfHeight) 
        {
            yOffset = -(halfHeight - parentCenterY) - parentHeight;
        }
        else 
        {
            yOffset = (parentCenterY - halfHeight) - parentHeight;
        }

        if (parentCenterX < halfWidth) 
        {         
            xOffset = -(halfWidth - parentCenterX);     
        }   

        if (parentCenterX >= halfWidth) 
        {
            xOffset = parentCenterX - halfWidth;
        }  
    }

    Toast toast = Toast.makeText(getActivity(), messageId, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, xOffset, yOffset);
    toast.show();       
}
//v是您希望Toast显示在上面的按钮视图
//messageId是消息的字符串资源的id
私有void显示到StabOVE按钮(视图v,int messageId)
{
int xOffset=0;
int-yOffset=0;
Rect gvr=新Rect();
视图父对象=(视图)v.getParent();
int parentHeight=parent.getHeight();
if(v.getGlobalVisibleRect(gvr))
{       
视图根=v.getRootView();
int halfWidth=root.getRight()/2;
int halfHeight=root.getBottom()/2;
int parentCenterX=((gvr.right-gvr.left)/2)+gvr.left;
int parentCenterY=((gvr.bottom-gvr.top)/2)+gvr.top;
if(parentCenterY=halfWidth)
{
xOffset=parentCenterX—半宽度;
}  
}
Toast Toast=Toast.makeText(getActivity(),messageId,Toast.LENGTH\u SHORT);
toast.setGravity(Gravity.CENTER,xOffset,yOffset);
toast.show();
}

在topin屏幕上设置toast

toast.setView(view);
toast.setGravity(Gravity.BOTTOM , 0, 0); // here i am setting toast at bottom
 toast.setDuration(Toast.LENGTH_LONG);
 toast.show(); 
现在在底部

 toast.setView(view);
 toast.setGravity(Gravity.BOTTOM , 0, 0); // here i am setting toast at bottom
 toast.setDuration(Toast.LENGTH_LONG);
 toast.show();  
同样的方法,我们可以把吐司放在左边,右边和中间


单击

更改土司颜色、位置和背景色的方法是:

Toast toast=Toast.makeText(getApplicationContext(),"This is advanced toast",Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT,0,0);
View view=toast.getView();
TextView  view1=(TextView)view.findViewById(android.R.id.message);
view1.setTextColor(Color.YELLOW);
view.setBackgroundResource(R.color.colorPrimary);
toast.show();

对于逐行解释:

//一个自定义toast类,您可以根据需要显示自定义或默认toast)

从:

警告:从Android Build.VERSION#CODES#R开始,对于目标为API级别Build.VERSION#CODES#R或更高版本的应用程序,此方法(setGravity)在文本祝酒时被调用时不起作用


这意味着
setGravity
不能再在API 30+中使用,必须找到另一个来实现所需的行为。

对于像我这样的人,正在寻找在Android R及更高版本上设置烤面包机重力的答案,请参阅


//这是对抗任何错误的最佳解决方案

makeText返回Toast对象,因此您可以简单地在makeText之后添加.addGravity和.show。“如果您收到指示必须调用makeText的错误”-何时会显示错误?如果您使用构造函数so
new Toast(上下文),则会发生错误
而不是
Toast.makeText(…)
那里的整数值是多少?他们是新闻部吗?或者max是什么?可能指出明显的,但是<代码>重力。CytLoad垂直/<代码>将图腾放在屏幕中间。X和Y偏移量是像素,所以最大值是显示宽度/高度。@ PTENUM10表示偏移量为像素。我应该假设这些是“px”单位,而不是“dp”单位吗?我在新的galaxy s6上的祝酒词显示在两个不同的位置。首先在左侧水平中心垂直,然后向下移动到中心水平,底部垂直。这种影响不会发生在我以前的任何测试设备上。我将每条消息连续显示两次,因为任何屏幕点击都会立即杀死第一个吐司。这个答案考虑了吐司的大小,这对我很有帮助。这是一个很好的例子。谢谢。虽然这个链接可以回答这个问题,但最好在这里包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能会变得无效。请不要只发布代码作为答案,还要解释代码的作用以及如何解决问题。有解释的答案通常更有帮助,质量更好,更有可能吸引选票。我想知道他们为什么这么做。。。
public class ToastMessage {
            private Context context;
            private static ToastMessage instance;

            /**
             * @param context
             */
            private ToastMessage(Context context) {
                this.context = context;
            }

            /**
             * @param context
             * @return
             */
            public synchronized static ToastMessage getInstance(Context context) {
                if (instance == null) {
                    instance = new ToastMessage(context);
                }
                return instance;
            }

            /**
             * @param message
             */
            public void showLongMessage(String message) {
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
            }

            /**
             * @param message
             */
            public void showSmallMessage(String message) {
                Toast.makeText(context, message, Toast.LENGTH_LONG).show();
            }

            /**
             * The Toast displayed via this method will display it for short period of time
             *
             * @param message
             */
            public void showLongCustomToast(String message) {
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                View layout = inflater.inflate(R.layout.layout_custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.ll_toast));
                TextView msgTv = (TextView) layout.findViewById(R.id.tv_msg);
                msgTv.setText(message);
                Toast toast = new Toast(context);
                toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();


            }

            /**
             * The toast displayed by this class will display it for long period of time
             *
             * @param message
             */
            public void showSmallCustomToast(String message) {

                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                View layout = inflater.inflate(R.layout.layout_custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.ll_toast));
                TextView msgTv = (TextView) layout.findViewById(R.id.tv_msg);
                msgTv.setText(message);
                Toast toast = new Toast(context);
                toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);
                toast.show();
            }

        }
Toast toast = Toast.makeText(this, "Custom toast creation", Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT,0,0);
    toast.show();
 Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);

 toast.setGravity(Gravity.CENTER, 0, 0);

 toast.show();