đây là ví dụ hoàn chỉnh, được sử dụng cho Bánh mì nướng tùy chỉnh trên các hoạt động.
displayToast
// display customized Toast message
public static int SHORT_TOAST = 0;
public static int LONG_TOAST = 1;
public static void displayToast(Context caller, String toastMsg, int toastType){
try {// try-catch to avoid stupid app crashes
LayoutInflater inflater = LayoutInflater.from(caller);
View mainLayout = inflater.inflate(R.layout.toast_layout, null);
View rootLayout = mainLayout.findViewById(R.id.toast_layout_root);
ImageView image = (ImageView) mainLayout.findViewById(R.id.image);
image.setImageResource(R.drawable.img_icon_notification);
TextView text = (TextView) mainLayout.findViewById(R.id.text);
text.setText(toastMsg);
Toast toast = new Toast(caller);
//toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setGravity(Gravity.BOTTOM, 0, 0);
if (toastType==SHORT_TOAST)//(isShort)
toast.setDuration(Toast.LENGTH_SHORT);
else
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(rootLayout);
toast.show();
}
catch(Exception ex) {// to avoid stupid app crashes
Log.w(TAG, ex.toString());
}
}
và toast_layout.xml
<?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>
Javacadabra Câu trả lời là tốt hơn trong quan điểm của tôi – rubdottocom
Tôi có đọc câu hỏi sai? Nhưng câu hỏi hỏi làm thế nào để ngăn chặn nó khỏi bị tùy chỉnh, và bạn đang nói CÁCH để tùy chỉnh nó? – WORMSS
@WORRMS, bạn nói đúng, nhưng ... Theo như chủ đề được thay đổi, bất kỳ bánh mì nướng nào không áp dụng chủ đề này là bánh mì nướng tùy chỉnh (vì chúng tôi cần "đổi kiểu" lại) – Dmitry