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

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

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

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

191746 次浏览

的文档,

烤面包的位置

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

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

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
如果你想将位置向右微调,增加的值 第二个参数。要使它下降,增加最后一个的值 参数。< / p >

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

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

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

注意:对于这个过程,你必须使用object of Toast

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

setGravity(int gravity, int xOffset, int yOffset)

< a href = " http://developer.android.com/reference/android/widget/Toast.html setGravity % 28 int, int % 20, % 20 int % 29”>文档< / >

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

关于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();
}
 Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 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类,可以显示自定义或默认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();
}


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

文档:

Warning:从Android Build开始。VERSION_CODES#R,用于针对API级别构建的应用程序。VERSION_CODES#R或更高,此方法(setGravity)在文本toast上调用时是一个无操作。

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

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


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


toast.show();

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

解决方案

fun Context.showToastOnTOP(message: String) = Toast.makeText(this, message, Toast.LENGTH_SHORT)
.apply { setGravity(Gravity.TOP, 0, 0); show() }


fun Context.showToastOnBOTTOM(message: String) = Toast.makeText(this, message, Toast.LENGTH_SHORT)
.apply { setGravity(Gravity.BOTTOM, 0, 0); show() }

重要提示:Gravity只会在你的应用目标最大29或更少的情况下工作。

冷静药丸:)