1.
(1)
public class ToastActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toast);
( (Button) findViewById(R.id.button1) ).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setTitle("短时间显示Toast");
showToast(Toast.LENGTH_SHORT);
}
});
( (Button) findViewById(R.id.button2) ).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setTitle("长时间显示Toast");
showToast(Toast.LENGTH_LONG);
}
});
}
protected void showToast(int type) {
Toast.makeText(this, "*********", Toast.LENGTH_LONG).show();
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.toast, null);
Toast toast = new Toast(this);
toast.setView(view);
toast.setDuration(type);
toast.show();
}
}
(2)toast.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:drawable/toast_frame"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/default_icon" /> <TextView android:id="@+id/content" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="6dip" android:text="你好, Android" /> </LinearLayout> </FrameLayout>
2.