Android Toast在開發中經常使用,多用于向使用者展示提示資訊,隻不過系統的Toast有些太單調了,不夠美觀,下面介紹一種超簡單的自定義Toast的方式:
在介紹自定義Toast之前,和大家一起看幾行系統Toast的源碼:
public static Toast makeText(Context context, CharSequence text, @Duration int duration){
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
這是系統Toast的mekeText()方法,大家天天都在用,源碼很簡單:new 一個Toast對象,加載一個布局頁面(一個簡單的TextView),把要顯示的内容設定到TextView上,方法傳回該Toast對象。[就這麼簡單]
我們自定義Toast其實隻想改變Toast的UI,讓它更美觀一些,甚至加一些我們的東西進去(圖示之類的),核心代碼就是makeText方法,通過修改這個方法完全可以随心所欲的定義我們的Toast。
下面說一下我自定義Toast的思路:
1.建立一個類Toast(類名與系統相同是為了我們的書寫習慣考慮)
2.定義makeText方法(定義與系統相同的makeText方法,核心是換成我們自定義的布局)
就是這麼簡單!看完代碼就明白了…
public class Toast {
public @interface Duration {
}
/**
* Show the view or text notification for a short period of time. This time
* could be user-definable. This is the default.
*/
public static final int LENGTH_SHORT = 0;
/**
* Show the view or text notification for a long period of time. This time
* could be user-definable.
*/
public static final int LENGTH_LONG = 1;
private Toast() {
}
/**
* Make a standard toast that just contains a text view.
*
* @param context The context to use. Usually your {@link android.app.Application}
* or {@link android.app.Activity} object.
* @param text The text to show. Can be formatted text.
* @param duration How long to display the message. Either {@link #LENGTH_SHORT} or
* {@link #LENGTH_LONG}
*/
public static android.widget.Toast makeText(Context context, CharSequence text, @Duration int duration) {
android.widget.Toast result = new android.widget.Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(R.layout.transient_notification, null);
TextView tv = (TextView) v.findViewById(R.id.message);
tv.setText(text);
result.setView(v);
result.setDuration(duration);
return result;
}
/**
* Make a standard toast that just contains a text view with the text from a resource.
*
* @param context The context to use. Usually your {@link android.app.Application}
* or {@link android.app.Activity} object.
* @param resId The resource id of the string resource to use. Can be formatted text.
* @param duration How long to display the message. Either {@link #LENGTH_SHORT} or
* {@link #LENGTH_LONG}
* @throws Resources.NotFoundException if the resource can't be found.
*/
public static android.widget.Toast makeText(Context context, @StringRes int resId, @Duration int duration)
throws Resources.NotFoundException {
return makeText(context, context.getResources().getText(resId), duration);
}
}
核心是makeText(Context context, CharSequence text, @Duration int duration)方法,仍然是建立系統Toast,關鍵點是加載我們自己的transient_notification.xml布局檔案,細心的朋友可能還會看到:處理根布局View和duration兩個變量,我們分别使用系統Toast提供的setView和setDuration兩個public方法。
transient_notification.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/toast_frame"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="@mipmap/qq"
android:drawableRight="@mipmap/qq"
android:shadowColor="#BB000000"
android:shadowRadius="2.75"
android:textAppearance="@style/TextAppearance.Toast"
android:textColor="#1bb5d7"/>
</LinearLayout>
需要什麼樣式的Toast,就定義自己的transient_notification.xml布局就可以,顯示圖檔還是文字完全由你掌控!
我們的Toast定義完了,怎麼用呢?
Toast.makeText(MainActivity.this, "自定義toast", Toast.LENGTH_LONG).show();
看到這行代碼有沒有很親切!沒錯,我們的自定義Toast就是這麼用的!
隻有一點不同,不要再導入系統的Toast類了,用自己定義的,連包都不需要導入了!
import android.widget.Toast;//删掉這句,無需導包
.apk安裝包下載下傳
Demo源碼下載下傳