Android Toast几种使用方法:
一.默认展示:
Toast toast=Toast.makeText(getApplicationContext(), "默认的Toast", Toast.LENGTH_SHORT);
解释:
(1).第一个参数:当前的上下文环境。可用getApplicationContext()或this
(2).第二个参数:要显示的字符串。也可是R.string中字符串ID
(3).第三个参数:显示的时间长短。Toast默认的有两个LENGTH_LONG(长)和LENGTH_SHORT(短),也可以使用毫秒如2000ms
toast.show();
二.自定义显示位置
Toast toast=Toast.makeText(getApplicationContext(), "自定义显示位置的Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER, -50, 100);
//屏幕居中显示,X轴和Y轴偏移量都是0
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
(1).第一个参数:设置toast在屏幕中显示的位置。我现在的设置是居中靠顶
(2).第二个参数:相对于第一个参数设置toast位置的横向X轴的偏移量,正数向右偏移,负数向左偏移
(3).第三个参数:同的第二个参数道理一样
如果你设置的偏移量超过了屏幕的范围,toast将在屏幕内靠近超出的那个边界显示
三、带图片的
Toast toast=Toast.makeText(getApplicationContext(), "显示带图片的toast", 3000);
toast.setGravity(Gravity.CENTER, 0, 0);
//创建图片视图对象
ImageView imageView= new ImageView(getApplicationContext());
//设置图片
imageView.setImageResource(R.drawable.ic_launcher);
//获得toast的布局
LinearLayout toastView = (LinearLayout) toast.getView();
//设置此布局为横向的
toastView.setOrientation(LinearLayout.HORIZONTAL);
//将ImageView在加入到此布局中的第一个位置
toastView.addView(imageView, 0);
toast.show();
四、完全自定义显示方式
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ToastActivity.java类:
public class ToastActivity extends Activity {
private Button bt;
private ImageView image;
private TextView title, content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToast();
}
});
}
private void showToast() {
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.toast, null);
image = (ImageView) view.findViewById(R.id.image);
title = (TextView) view.findViewById(R.id.title);
content = (TextView) view.findViewById(R.id.content);
image.setBackgroundResource(R.drawable.ic_launcher);
title.setText("自定义toast");
content.setText("hello,self toast");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
toast.show();
}
}
五、其他线程通过Handler的调用
//调用方法1
//Thread th=new Thread(this);
//th.start();
//调用方法2
handler.post(new Runnable() {
@Override
public void run() {
showToast();
}
});
public void showToast(){
Toast toast=Toast.makeText(getApplicationContext(), "Toast在其他线程中调用显示", Toast.LENGTH_SHORT);
toast.show();
}
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
int what=msg.what;
switch (what) {
case 1:
showToast();
break;
default:
break;
}
super.handleMessage(msg);
}
};
@Override
public void run() {
handler.sendEmptyMessage(1);
}
Util.java类:
package com.chengdong.su.toastutil;
import android.content.Context;
import android.support.v4.widget.DrawerLayout.LayoutParams;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Util {
private static Toast mToast = null;
public static int LENGTH_LONG = Toast.LENGTH_LONG;
private static int LENGTH_SHORT = Toast.LENGTH_SHORT;
/**
* 普通文本消息提示
*
* @param context
* @param text
* @param duration
*/
public static void TextToast(Context context, CharSequence text,
int duration) {
// 创建一个Toast提示消息
mToast = Toast.makeText(context, text, duration);
// 设置Toast提示消息在屏幕上的位置
mToast.setGravity(Gravity.CENTER, 0, 0);
// 显示消息
mToast.show();
}
/**
* 带图片消息提示
*
* @param context
* @param ImageResourceId
* @param text
* @param duration
*/
public static void ImageToast(Context context, int imageResourceId,
CharSequence text, int duration) {
// 创建一个Toast提示消息
mToast = Toast.makeText(context, text, Toast.LENGTH_LONG);
// 设置Toast提示消息在屏幕上的位置
mToast.setGravity(Gravity.CENTER, 0, 0);
// 获取Toast提示消息里原有的View
View toastView = mToast.getView();
// 创建一个ImageView
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageResourceId);
// 创建一个LineLayout容器
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// 向LinearLayout中添加ImageView和Toast原有的View
linearLayout.addView(imageView);
linearLayout.addView(toastView);
// 将LineLayout容器设置为toast的View
mToast.setView(linearLayout);
// 显示消息
mToast.show();
}
}