準備材料:
自定義Toast的佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_people" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是自定義吐司"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
開始使用:
從源碼中直接複製稍作修改,稍作修改
View v = inflate.inflate(R.layout.MyToast, null);
TextView tv = (TextView) v.findViewById(R.id.textView1);
public static void MyToast(Context context, CharSequence text, int i) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(R.layout.MyToast, null);
TextView tv = (TextView) v.findViewById(R.id.textView1);
tv.setText(text);
result.setView(v);
result.setDuration(i);
result.show();
}