方法一:
setGravity(int gravity, int xOffset, int yOffset)三个参数分别表示(起点位置,水平向右位移,垂直向下位移)方法二:
setMargin(float horizontalMargin, float verticalMargin)以横向和纵向的百分比设置显示位置,参数均为float类型(水平位移正右负左,竖直位移正上负下)
Java代码
// 设置Toast显示位置(起点位置,水平向右位移,垂直向下位移)
toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 200);
// Toast显示位置,以横向和纵向的百分比计算,参数均为float类型(水平位移正右负左,竖直位移正上负下)
toast.setMargin(-0.5f, 0f);
自定义Toast
下面这段代码可以显示一个带图片的Toast效果:// 带图片的Toast
Button btn2 = (Button) findViewById(R.id.toast2);
btn2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 定义一个Toast
Toast toast = Toast.makeText(ToastDemoActivity.this, "这是一个代图片的Toast!", Toast.LENGTH_LONG);
// 定义一个ImageView
ImageView imageView = new ImageView(ToastDemoActivity.this);
imageView.setImageResource(R.drawable.icon);
// 获得Toast的View
View toastView = toast.getView();
// 定义一个Layout,这里是Layout
LinearLayoutlinear Layout = new LinearLayout(ToastDemoActivity.this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// 将ImageView和ToastView合并到Layout中
linearLayout.addView(imageView);
linearLayout.addView(toastView);
// 替换掉原有的ToastView
toast.setView(linearLayout);
toast.show();
}
});
完全自定义代码如下:LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom,
(ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView) layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
以上就是“吐司"了,很简单的东西了,高手就不用看了..........