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

时间:2010-03-24 10:31:57

标签: android toast

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

现在我想根据我的选择在屏幕中间或某处显示它。

任何人都可以指导我如何实现这一目标吗?

12 个答案:

答案 0 :(得分:378)

来自the documentation

  

定位敬酒

     

屏幕底部附近会出现标准吐司通知,   水平居中。你可以改变这个位置   setGravity(int, int, int)方法。这接受三个参数:a   Gravity常量,x-position偏移量和y-position偏移量。

     

例如,如果你决定吐司应该出现在   左上角,您可以像这样设置重力:

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
     

如果要将位置微移到右侧,请增加值   第二个参数。要轻推它,增加最后一个的值   参数。

答案 1 :(得分:133)

另外,如果您收到错误消息,表明您必须调用makeText,则以下代码可以使其正常工作:

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

答案 2 :(得分:13)

您可以使用以下方式自定义Toast的位置:

setGravity(int gravity, int xOffset, int yOffset)

docs

这使您可以非常具体地了解Toast的位置。

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

例如,如果要创建一个出现在Button顶部的自定义Toast,您可以创建如下函数:

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

答案 3 :(得分:8)

 Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);
 toast.setGravity(Gravity.CENTER, 0, 0);
 toast.show();

答案 4 :(得分:6)

Toast mytoast= Toast.makeText(getApplicationContext(), "Toast Message", 1);  
mytoast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);  // for center horizontal
//mytoast.setGravity(Gravity.CENTER_VERTICAL);       // for center vertical 
//mytoast.setGravity(Gravity.TOP);                       // for top
mytoast.show();

以上代码将帮助您在屏幕中间显示吐司或根据您的选择,根据您的需要设置吐司重力

注意:对于此过程,您必须使用Toast对象

答案 5 :(得分:2)

改变吐司的颜色,位置和背景颜色的方法是:

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

对于逐行说明:https://www.youtube.com/watch?v=5bzhGd1HZOc

答案 6 :(得分:1)

在topin屏幕设置烤面包

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

我们可以在左,右和中心设置烤面包的方式相同

点击here

答案 7 :(得分:1)

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

答案 8 :(得分:1)

对于像我这样想要在 Android R 及更高版本上设置 Toast 重力的答案,请参阅this article

答案 9 :(得分:1)

 Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);

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

 toast.show();

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

答案 10 :(得分:0)

//自定义Toast类,您可以根据需要显示自定义或默认吐司)

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

        }

答案 11 :(得分:0)

来自documentation

警告:从Android Build.VERSION_CODES#R开始,对于定位到API级别Build.VERSION_CODES#R或更高版本的应用,此方法( setGravity )是调用文本吐司时不进行操作。

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