如何在Android中更改Toast的位置?
当我使用Toast在屏幕上显示一些弹出文本时,它会在屏幕底部稍上方显示文本,这是默认位置。
现在我想根据我的选择在屏幕中间或某处显示它。
谁能指导我如何实现这一目标?
UMAR asked 2019-02-20T14:13:36Z
8个解决方案
362 votes
从文档中,
定位你的吐司
标准吐司通知出现在屏幕底部附近, 水平居中。 你可以改变这个位置 toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);方法。 这接受三个参数:a Gravity常数,偏移量为x-position,偏移量为y-position。
例如,如果您确定吐司应该出现在 左上角,您可以像这样设置重力:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
如果要将位置微移到右侧,请增加值 第二个参数。 要轻推它,增加最后一个的值 参数。
Pentium10 answered 2019-02-20T14:14:39Z
126 votes
顺便说一句,如果您收到错误消息,表明您必须调用makeText,则以下代码使其工作:
Toast toast= Toast.makeText(getApplicationContext(),
"Your string here", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
Rymnel answered 2019-02-20T14:15:08Z
12 votes
您可以使用以下方法自定义Toast的位置:
setGravity(int gravity, int xOffset, int yOffset)
文档
这使您可以非常具体地了解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();
}
JDJ answered 2019-02-20T14:16:27Z
6 votes
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对象
user3652986 answered 2019-02-20T14:17:00Z
6 votes
Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
nzala answered 2019-02-20T14:17:20Z
2 votes
改变吐司的颜色,位置和背景颜色的方法是:
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]
Mithun Adhikari answered 2019-02-20T14:18:39Z
1 votes
在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();
我们可以在左,右和中心设置烤面包
点击这里
Abhishek answered 2019-02-20T14:19:56Z
0 votes
//自定义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();
}
}
Amardeep answered 2019-02-20T14:20:36Z