如何更改 Toast 在 Android 中的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2506876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change position of Toast in Android?
提问by UMAR
When I use Toast
to display some popup text on the screen, it displays the text a little bit above the bottom of the screen, which is the default position.
当我用来Toast
在屏幕上显示一些弹出文本时,它会在屏幕底部上方一点点显示文本,这是默认位置。
Now I want to display it in the middle of screen or somewhere according to my choice.
现在我想根据我的选择将它显示在屏幕中间或某处。
Can anyone guide me how to achieve this?
谁能指导我如何实现这一目标?
回答by Pentium10
From the documentation,
从文档中,
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: aGravity
constant, anx-position
offset, and ay-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 通知出现在屏幕底部附近,水平居中。您可以使用
setGravity(int, int, int)
方法更改此位置 。这接受三个参数:Gravity
常量、x-position
偏移量和y-position
偏移量。例如,如果你决定 toast 应该出现在左上角,你可以像这样设置重力:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
如果要向右轻推位置,请增加第二个参数的值。要向下微调,请增加最后一个参数的值。
回答by Rymnel
If you get an error indicating that you must call makeText, the following code will fix it:
如果你收到一个错误,表明你必须调用 makeText,下面的代码将修复它:
Toast toast= Toast.makeText(getApplicationContext(),
"Your string here", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
回答by JDJ
You can customize the location of your Toast by using:
您可以使用以下方法自定义 Toast 的位置:
setGravity(int gravity, int xOffset, int yOffset)
setGravity(int gravity, int xOffset, int yOffset)
This allows you to be very specific about where you want the location of your Toast to be.
这使您可以非常具体地说明您希望 Toast 的位置。
One of the most useful things about the xOffset and yOffset parameters is that you can use them to place the Toast relative to a certain View.
关于 xOffset 和 yOffset 参数的最有用的事情之一是您可以使用它们来相对于某个视图放置 Toast。
For example, if you want to make a custom Toast that appears on top of a Button, you could create a function like this:
例如,如果您想制作一个出现在 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();
}
回答by nzala
Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
回答by user3652986
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();
The above code is will help u to display toast in the middle of screen or according to ur choice for that just set the toast gravity according to ur need
上面的代码将帮助你在屏幕中间显示吐司,或者根据你的选择,根据你的需要设置吐司重力
Note: For this process u have to use object of Toast
注意:对于此过程,您必须使用 Toast 对象
回答by Mithun Adhikari
The method to change the color, position and background color of toast is:
改变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();
For line by line explanation: https://www.youtube.com/watch?v=5bzhGd1HZOc
回答by Abhishek
setting toast at topin screen
在 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();
now at bottom
现在在底部
toast.setView(view);
toast.setGravity(Gravity.BOTTOM , 0, 0); // here i am setting toast at bottom
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
the same way we can set toast in left, right and also center
就像我们可以在左侧、右侧和中间设置吐司一样
Click here
点击这里
回答by Amardeep
//A custom toast class where you can show custom or default toast as desired)
//自定义 toast 类,您可以在其中根据需要显示自定义或默认 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();
}
}