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

现在我想把它显示在屏幕中间,或者根据我的选择显示在某个地方。

有人能指导我如何实现这个目标吗?


当前回答

改变土司颜色、位置和底色的方法是:

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

其他回答

如果你得到一个指示你必须调用makeText的错误,下面的代码将修复它:

Toast toast= Toast.makeText(getApplicationContext(), 
"Your string here", Toast.LENGTH_SHORT);  
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
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();

逐行解释:https://www.youtube.com/watch?v=5bzhGd1HZOc

您可以使用以下命令自定义Toast的位置:

setGravity(int gravity, int xOffset, int yOffset)

docs

这让你可以非常明确你想要吐司的位置。

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

例如,如果你想要创建一个显示在按钮顶部的自定义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();       
}

从文档来看,

Positioning your Toast A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset. For example, if you decide that the toast should appear in the top-left corner, you can set the gravity like this: toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0); If you want to nudge the position to the right, increase the value of the second parameter. To nudge it down, increase the value of the last parameter.

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