Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。
1、默认效果:
- Toast toast = Toast.makeText(getApplicationContext(), "默认Toast样式",Toast.LENGTH_SHORT);
- toast.show();
2、自定义显示位置效果:
- Toast toast = Toast.makeText(getApplicationContext(),"自定义位置Toast", Toast.LENGTH_LONG);
- toast.setGravity(Gravity.CENTER, 0, 0);
- toast.show();
3、带图片效果:
- Toast toast = Toast.makeText(getApplicationContext(),"带图片的Toast", Toast.LENGTH_LONG);
- toast.setGravity(Gravity.CENTER, 0, 0);
- LinearLayout toastView = (LinearLayout) toast.getView();
- ImageView imageCodeProject = new ImageView(getApplicationContext());
- imageCodeProject.setImageResource(R.drawable.icon);
- toastView.addView(imageCodeProject, 0);
- toast.show();
4、完全自定义效果:
- 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 toast = new Toast(getApplicationContext());
- toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
- toast.setDuration(Toast.LENGTH_LONG);
- toast.setView(layout);
- toast.show();
5、其他线程:
- public void login(){
- new Thread(){
- public void run(){
- showToast();
- }
- }.start();
- }
- //方法:在新的线程中使用Toast返回用户登录消息提示
- Handler handler = new Handler();
- public void showToast() {
- handler.post(new Runnable() {
- @Override
- public void run() {
- Toast toast = Toast.makeText(LoginActivity.this, "请输入账号或密码", Toast.LENGTH_LONG);
- toast.show();
- }
- });
- }
Android系统默认的Toast十分简洁,使用也非常的简单。但是有时我们的程序使用默认的Toast时会和程序的整体风格不搭配,这个时候我们就需要自定义Toast,使其与我们的程序更加融合。
使用自定义Toast,首先我们需要添加一个布局文件,该布局文件的结构和Activity使用的布局文件结构一致,在该布局文件中我们需设计我们Toast的布局,例如:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/toast_layout_root"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding="10dp"
- android:background="#DAAA"
- >
- <ImageView android:id="@+id/image"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginRight="10dp"
- />
- <TextView android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:textColor="#FFF"
- />
- </LinearLayout>
在这个地方要注意,我们给LinearLayout添加的id属性,在后面的代码中我们需要使用到。在程序中,我们可以通过如下代码创建我们自己的Toast。
- /**
- * @author coolszy
- * @blog http://blog.csdn.net/coolszy
- */
- public class MainActivity extends Activity
- {
- private Button btn;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btn = (Button) findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- //获取LayoutInflater对象,该对象能把XML文件转换为与之一直的View对象
- LayoutInflater inflater = getLayoutInflater();
- //根据指定的布局文件创建一个具有层级关系的View对象
- //第二个参数为View对象的根节点,即LinearLayout的ID
- View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
- //查找ImageView控件
- //注意是在layout中查找
- ImageView image = (ImageView) layout.findViewById(R.id.image);
- image.setImageResource(R.drawable.head);
- TextView text = (TextView) layout.findViewById(R.id.text);
- text.setText("自定义Toast演示程序");
- Toast toast = new Toast(getApplicationContext());
- //设置Toast的位置
- toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
- toast.setDuration(Toast.LENGTH_LONG);
- //让Toast显示为我们自定义的样子
- toast.setView(layout);
- toast.show();
- }
- });
- }
- }
运行效果: