Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast。 DHS平坦软件园
1.默认效果DHS平坦软件园
DHS平坦软件园
代码DHS平坦软件园
Toast.makeText(getApplicationContext(), "默认Toast样式", DHS平坦软件园
Toast.LENGTH_SHORT).show(); DHS平坦软件园
2.自定义显示位置效果DHS平坦软件园
DHS平坦软件园
代码DHS平坦软件园
toast = Toast.makeText(getApplicationContext(), DHS平坦软件园
"自定义位置Toast", Toast.LENGTH_LONG); DHS平坦软件园
toast.setGravity(Gravity.CENTER, 0, 0); DHS平坦软件园
toast.show(); DHS平坦软件园
3.带图片效果DHS平坦软件园
DHS平坦软件园
代码DHS平坦软件园
toast = Toast.makeText(getApplicationContext(), DHS平坦软件园
"带图片的Toast", Toast.LENGTH_LONG); DHS平坦软件园
toast.setGravity(Gravity.CENTER, 0, 0); DHS平坦软件园
LinearLayout toastView = (LinearLayout) toast.getView(); DHS平坦软件园
ImageView imageCodeProject = new ImageView(getApplicationContext()); DHS平坦软件园
imageCodeProject.setImageResource(R.drawable.icon); DHS平坦软件园
toastView.addView(imageCodeProject, 0); DHS平坦软件园
toast.show(); DHS平坦软件园
4.完全自定义效果DHS平坦软件园
DHS平坦软件园
代码DHS平坦软件园
LayoutInflater inflater = getLayoutInflater(); DHS平坦软件园
View layout = inflater.inflate(R.layout.custom, DHS平坦软件园
(ViewGroup) findViewById(R.id.llToast)); DHS平坦软件园
ImageView image = (ImageView) layout DHS平坦软件园
.findViewById(R.id.tvImageToast); DHS平坦软件园
image.setImageResource(R.drawable.icon); DHS平坦软件园
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast); DHS平坦软件园
title.setText("Attention"); DHS平坦软件园
TextView text = (TextView) layout.findViewById(R.id.tvTextToast); DHS平坦软件园
text.setText("完全自定义Toast"); DHS平坦软件园
toast = new Toast(getApplicationContext()); DHS平坦软件园
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40); DHS平坦软件园
toast.setDuration(Toast.LENGTH_LONG); DHS平坦软件园
toast.setView(layout); DHS平坦软件园
toast.show(); DHS平坦软件园
5.其他线程DHS平坦软件园
DHS平坦软件园
代码DHS平坦软件园
new Thread(new Runnable() { DHS平坦软件园
public void run() { DHS平坦软件园
showToast(); DHS平坦软件园
} DHS平坦软件园
}).start(); DHS平坦软件园
完整代码DHS平坦软件园
1.Main,javaDHS平坦软件园
package com.wjq.toast; DHS平坦软件园
import android.app.Activity; DHS平坦软件园
import android.os.Bundle; DHS平坦软件园
import android.os.Handler; DHS平坦软件园
import android.view.Gravity; DHS平坦软件园
import android.view.LayoutInflater; DHS平坦软件园
import android.view.View; DHS平坦软件园
import android.view.ViewGroup; DHS平坦软件园
import android.view.View.OnClickListener; DHS平坦软件园
import android.widget.ImageView; DHS平坦软件园
import android.widget.LinearLayout; DHS平坦软件园
import android.widget.TextView; DHS平坦软件园
import android.widget.Toast; DHS平坦软件园
public class Main extends Activity implements OnClickListener { DHS平坦软件园
Handler handler = new Handler(); DHS平坦软件园
@Override DHS平坦软件园
public void onCreate(Bundle savedInstanceState) { DHS平坦软件园
super.onCreate(savedInstanceState); DHS平坦软件园
setContentView(R.layout.main); DHS平坦软件园
findViewById(R.id.btnSimpleToast).setOnClickListener(this); DHS平坦软件园
findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener( DHS平坦软件园
this); DHS平坦软件园
findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this); DHS平坦软件园
findViewById(R.id.btnCustomToast).setOnClickListener(this); DHS平坦软件园
findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this); DHS平坦软件园
} DHS平坦软件园
public void showToast() { DHS平坦软件园
handler.post(new Runnable() { DHS平坦软件园
@Override DHS平坦软件园
public void run() { DHS平坦软件园
Toast.makeText(getApplicationContext(), "我来自其他线程!", DHS平坦软件园
Toast.LENGTH_SHORT).show(); DHS平坦软件园
} DHS平坦软件园
}); DHS平坦软件园
} DHS平坦软件园
@Override DHS平坦软件园
public void onClick(View v) { DHS平坦软件园
Toast toast = null; DHS平坦软件园
switch (v.getId()) { DHS平坦软件园
case R.id.btnSimpleToast: DHS平坦软件园
Toast.makeText(getApplicationContext(), "默认Toast样式", DHS平坦软件园
Toast.LENGTH_SHORT).show(); DHS平坦软件园
break; DHS平坦软件园
case R.id.btnSimpleToastWithCustomPosition: DHS平坦软件园
toast = Toast.makeText(getApplicationContext(), DHS平坦软件园
"自定义位置Toast", Toast.LENGTH_LONG); DHS平坦软件园
toast.setGravity(Gravity.CENTER, 0, 0); DHS平坦软件园
toast.show(); DHS平坦软件园
break; DHS平坦软件园
case R.id.btnSimpleToastWithImage: DHS平坦软件园
toast = Toast.makeText(getApplicationContext(), DHS平坦软件园
"带图片的Toast", Toast.LENGTH_LONG); DHS平坦软件园
toast.setGravity(Gravity.CENTER, 0, 0); DHS平坦软件园
LinearLayout toastView = (LinearLayout) toast.getView(); DHS平坦软件园
ImageView imageCodeProject = new ImageView(getApplicationContext()); DHS平坦软件园
imageCodeProject.setImageResource(R.drawable.icon); DHS平坦软件园
toastView.addView(imageCodeProject, 0); DHS平坦软件园
toast.show(); DHS平坦软件园
break; DHS平坦软件园
case R.id.btnCustomToast: DHS平坦软件园
LayoutInflater inflater = getLayoutInflater(); DHS平坦软件园
View layout = inflater.inflate(R.layout.custom, DHS平坦软件园
(ViewGroup) findViewById(R.id.llToast)); DHS平坦软件园
ImageView image = (ImageView) layout DHS平坦软件园
.findViewById(R.id.tvImageToast); DHS平坦软件园
image.setImageResource(R.drawable.icon); DHS平坦软件园
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast); DHS平坦软件园
title.setText("Attention"); DHS平坦软件园
TextView text = (TextView) layout.findViewById(R.id.tvTextToast); DHS平坦软件园
text.setText("完全自定义Toast"); DHS平坦软件园
toast = new Toast(getApplicationContext()); DHS平坦软件园
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40); DHS平坦软件园
toast.setDuration(Toast.LENGTH_LONG); DHS平坦软件园
toast.setView(layout); DHS平坦软件园
toast.show(); DHS平坦软件园
break; DHS平坦软件园
case R.id.btnRunToastFromOtherThread: DHS平坦软件园
new Thread(new Runnable() { DHS平坦软件园
public void run() { DHS平坦软件园
showToast(); DHS平坦软件园
} DHS平坦软件园
}).start(); DHS平坦软件园
break; DHS平坦软件园
} DHS平坦软件园
} DHS平坦软件园
} DHS平坦软件园
2.main,xml DHS平坦软件园
<?xml version="1.0" encoding="utf-8"?> DHS平坦软件园
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android " DHS平坦软件园
android:orientation="vertical" android:layout_width="fill_parent" DHS平坦软件园
android:layout_height="fill_parent" android:padding="5dip" android:gravity="center"> DHS平坦软件园
<Button android:layout_height="wrap_content" DHS平坦软件园
android:layout_width="fill_parent" android:id="@+id/btnSimpleToast" DHS平坦软件园
android:text="默认"></Button> DHS平坦软件园
<Button android:layout_height="wrap_content" DHS平坦软件园
android:layout_width="fill_parent" android:text="自定义显示位置" DHS平坦软件园
android:id="@+id/btnSimpleToastWithCustomPosition"></Button> DHS平坦软件园
<Button android:layout_height="wrap_content" DHS平坦软件园
android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage" DHS平坦软件园
android:text="带图片"></Button> DHS平坦软件园
<Button android:layout_height="wrap_content" DHS平坦软件园
android:layout_width="fill_parent" android:text="完全自定义" DHS平坦软件园
android:id="@+id/btnCustomToast"></Button> DHS平坦软件园
<Button android:layout_height="wrap_content" DHS平坦软件园
android:layout_width="fill_parent" android:text="其他线程" DHS平坦软件园
android:id="@+id/btnRunToastFromOtherThread"></Button> DHS平坦软件园
</LinearLayout> DHS平坦软件园
3.custom.xml DHS平坦软件园
<?xml version="1.0" encoding="utf-8"?> DHS平坦软件园
<LinearLayout DHS平坦软件园
xmlns:android="http://schemas.android.com/apk/res/android " DHS平坦软件园
android:layout_height="wrap_content" android:layout_width="wrap_content" DHS平坦软件园
android:background="#ffffffff" android:orientation="vertical" DHS平坦软件园
android:id="@+id/llToast" > DHS平坦软件园
<TextView DHS平坦软件园
android:layout_height="wrap_content" DHS平坦软件园
android:layout_margin="1dip" DHS平坦软件园
android:textColor="#ffffffff" DHS平坦软件园
android:layout_width="fill_parent" DHS平坦软件园
android:gravity="center" DHS平坦软件园
android:background="#bb000000" DHS平坦软件园
android:id="@+id/tvTitleToast" /> DHS平坦软件园
<LinearLayout DHS平坦软件园
android:layout_height="wrap_content" DHS平坦软件园
android:orientation="vertical" DHS平坦软件园
android:id="@+id/llToastContent" DHS平坦软件园
android:layout_marginLeft="1dip" DHS平坦软件园
android:layout_marginRight="1dip" DHS平坦软件园
android:layout_marginBottom="1dip" DHS平坦软件园
android:layout_width="wrap_content" DHS平坦软件园
android:padding="15dip" DHS平坦软件园
android:background="#44000000" > DHS平坦软件园
<ImageView DHS平坦软件园
android:layout_height="wrap_content" DHS平坦软件园
android:layout_gravity="center" DHS平坦软件园
android:layout_width="wrap_content" DHS平坦软件园
android:id="@+id/tvImageToast" /> DHS平坦软件园
<TextView DHS平坦软件园
android:layout_height="wrap_content" DHS平坦软件园
android:paddingRight="10dip" DHS平坦软件园
android:paddingLeft="10dip" DHS平坦软件园
android:layout_width="wrap_content" DHS平坦软件园
android:gravity="center" DHS平坦软件园
android:textColor="#ff000000" DHS平坦软件园
android:id="@+id/tvTextToast" /> DHS平坦软件园
</LinearLayout> DHS平坦软件园
</LinearLayout>