Answers:
从文档中,
定位吐司
在屏幕底部附近,水平居中显示标准的敬酒通知。您可以使用
setGravity(int, int, int)
方法更改此位置 。它接受三个参数:Gravity
常数,x-position
偏移量和y-position
偏移量。例如,如果您决定将吐司面包显示在左上角,则可以这样设置重力:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
如果要向右微移位置,请增加第二个参数的值。要微调它,请增加最后一个参数的值。
Gravity.CENTER_VERTICAL
会将吐司放在屏幕中间。
如果收到指示必须调用makeText的错误消息,则以下代码将对其进行修复:
Toast toast= Toast.makeText(getApplicationContext(),
"Your string here", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
new Toast(context)
而不是Toast.makeText(...)
您可以使用以下方法自定义Toast的位置:
setGravity(int gravity, int xOffset, int yOffset)
这使您可以非常具体地确定Toast的位置。
关于xOffset和yOffset参数最有用的事情之一是,您可以使用它们相对于某个View放置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();
}
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对象
更改烤面包的颜色,位置和背景颜色的方法是:
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();
在图钉屏幕上设置吐司
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();
我们可以在左,右和中间设置烤面包的方式相同
点击这里
//一个自定义吐司类,您可以根据需要显示自定义或默认吐司)
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();
}
}