分享Android中Toast的自定義使用
1.Toast源碼分析
老規(guī)矩,我們先去看Toast的源碼。
Toast有兩種顯示布局方式,一種最常見調(diào)用Toast.makeText() ,看源碼是這樣寫的
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;
}
transient_notification這個(gè)布局文件代碼是這樣的
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="?android:attr/toastFrameBackground"> <TextView android:id="@android:id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_horizontal" android:textAppearance="@style/TextAppearance.Toast" android:textColor="@color/bright_foreground_dark" android:shadowColor="#BB000000" android:shadowRadius="2.75" /> </LinearLayout>
那么我們想要修改Toast的文字消息樣式,其實(shí)就是修改Toast根布局和message這個(gè)TextView。
Toast的另外一種顯示模式就是自定義布局顯示。這個(gè)方法不調(diào)用Toast.makeText()方法,而是new一個(gè)Toast對(duì)象,然后調(diào)用setView()方法。當(dāng)然自定義布局就不會(huì)加載transient_notification布局了。
2.實(shí)現(xiàn)自定義Toast
先給大家看下我封裝的工具類ToastUtil。
import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by 趙晨璞 on 2016/8/11.
*/
public class ToastUtil {
private Toast toast;
private LinearLayout toastView;
/**
* 修改原布局的Toast
*/
public ToastUtil() {
}
/**
* 完全自定義布局Toast
* @param context
* @param view
*/
public ToastUtil(Context context, View view,int duration){
toast=new Toast(context);
toast.setView(view);
toast.setDuration(duration);
}
/**
* 向Toast中添加自定義view
* @param view
* @param postion
* @return
*/
public ToastUtil addView(View view,int postion) {
toastView = (LinearLayout) toast.getView();
toastView.addView(view, postion);
return this;
}
/**
* 設(shè)置Toast字體及背景顏色
* @param messageColor
* @param backgroundColor
* @return
*/
public ToastUtil setToastColor(int messageColor, int backgroundColor) {
View view = toast.getView();
if(view!=null){
TextView message=((TextView) view.findViewById(android.R.id.message));
message.setBackgroundColor(backgroundColor);
message.setTextColor(messageColor);
}
return this;
}
/**
* 設(shè)置Toast字體及背景
* @param messageColor
* @param background
* @return
*/
public ToastUtil setToastBackground(int messageColor, int background) {
View view = toast.getView();
if(view!=null){
TextView message=((TextView) view.findViewById(android.R.id.message));
message.setBackgroundResource(background);
message.setTextColor(messageColor);
}
return this;
}
/**
* 短時(shí)間顯示Toast
*/
public ToastUtil Short(Context context, CharSequence message){
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message, Toast.LENGTH_SHORT);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(Toast.LENGTH_SHORT);
}
return this;
}
/**
* 短時(shí)間顯示Toast
*/
public ToastUtil Short(Context context, int message) {
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message, Toast.LENGTH_SHORT);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(Toast.LENGTH_SHORT);
}
return this;
}
/**
* 長(zhǎng)時(shí)間顯示Toast
*/
public ToastUtil Long(Context context, CharSequence message){
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message, Toast.LENGTH_LONG);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(Toast.LENGTH_LONG);
}
return this;
}
/**
* 長(zhǎng)時(shí)間顯示Toast
*
* @param context
* @param message
*/
public ToastUtil Long(Context context, int message) {
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message, Toast.LENGTH_LONG);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(Toast.LENGTH_LONG);
}
return this;
}
/**
* 自定義顯示Toast時(shí)間
*
* @param context
* @param message
* @param duration
*/
public ToastUtil Indefinite(Context context, CharSequence message, int duration) {
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message,duration);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(duration);
}
return this;
}
/**
* 自定義顯示Toast時(shí)間
*
* @param context
* @param message
* @param duration
*/
public ToastUtil Indefinite(Context context, int message, int duration) {
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message,duration);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(duration);
}
return this;
}
/**
* 顯示Toast
* @return
*/
public ToastUtil show (){
toast.show();
return this;
}
/**
* 獲取Toast
* @return
*/
public Toast getToast(){
return toast;
}
}
修改Toast背景色的使用法方法如下:
ToastUtil toastUtil=new ToastUtil(); toastUtil.Short(MainActivity.this,"自定義message字體、背景色").setToastColor(Color.WHITE, getResources().getColor(R.color.colorAccent)).show();

修改Toast背景色
方形的Toast看上去有些呆板,我自定義了一個(gè)名為toast_radius.xml的背景,代碼如下:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 填充的顏色 --> <solid android:color="#ffc107" /> <!-- android:radius 弧形的半徑 --> <corners android:radius="20dip" /> </shape>
然后上面設(shè)置背景的代碼改成:
toastUtil.Short(MainActivity.this,"自定義message字體顏色和背景").setToastBackground(Color.WHITE,R.drawable.toast_radius).show();

修改了背景的Toast
雖然官方認(rèn)為Toast和Snackbar都應(yīng)該是短文本的形式,不能包含圖標(biāo),但是個(gè)人感覺加上圖標(biāo)還是挺好玩的...
向Toast中添加圖標(biāo)可以這樣:
ImageView toastImage = new ImageView(getApplicationContext()); toastImage.setImageResource(R.mipmap.ic_launcher); toastUtil.Short(MainActivity.this,"向Toast添加了一個(gè)ImageView").setToastBackground(Color.WHITE,R.drawable.toast_radius).addView(toastImage,0).show();

添加圖標(biāo)的Toast
如果你想要Toast顯示自定義的布局,可以這樣:
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.image,null); new ToastUtil(MainActivity.this,view,Toast.LENGTH_SHORT).show();

自定義布局Toast,我的布局文件中只有一個(gè)默認(rèn)圖標(biāo)的ImageView
大家都知道,連續(xù)觸發(fā)Toast的show()方法的時(shí)候,Toast就會(huì)排著隊(duì)連續(xù)展示,感覺上不太友好。所以我先判斷了toast是否沒被創(chuàng)建或者是否被添加了額外的view,如果是的話就重新生成一個(gè)toast對(duì)象;如果否的話就只修改message文字和顯示時(shí)間。

Toast布局修改,不排隊(duì)顯示
總結(jié)
我這個(gè)工具類不是完全體,大家再根據(jù)自己項(xiàng)目的具體需求進(jìn)行修改。以上就是Android中Toast的花式使用的全部?jī)?nèi)容,感興趣的小伙伴們快快自己動(dòng)手實(shí)踐起來吧。
相關(guān)文章
淺談Android onTouchEvent 與 onInterceptTouchEvent的區(qū)別詳解
本篇文章小編為大家介紹,Android onTouchEvent 與 onInterceptTouchEvent的區(qū)別詳解。需要的朋友參考下2013-04-04
Android中手機(jī)錄屏并轉(zhuǎn)換GIF的兩種方式
本文主要介紹了android中手機(jī)錄屏并轉(zhuǎn)換GIF的兩種方式,具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01
Android 點(diǎn)擊ImageButton時(shí)有“按下”的效果的實(shí)現(xiàn)
這篇文章主要介紹了 Android 點(diǎn)擊ImageButton時(shí)有“按下”的效果的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android音頻系統(tǒng)AudioTrack使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android音頻系統(tǒng)AudioTrack的使用方法,如何使用AudioTrack進(jìn)行音頻播放,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
利用Flutter實(shí)現(xiàn)“孔雀開屏”的動(dòng)畫效果
這篇文章主要給大家介紹了關(guān)于如何利用Flutter實(shí)現(xiàn)“孔雀開屏”的動(dòng)畫效果,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Android自定義動(dòng)態(tài)壁紙開發(fā)詳解
這篇文章主要為大家詳細(xì)介紹了Android自定義動(dòng)態(tài)壁紙開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Android編程實(shí)現(xiàn)TextView部分顏色變動(dòng)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)TextView部分顏色變動(dòng)的方法,涉及Android針對(duì)TextView樣式操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android監(jiān)聽輸入法彈窗和關(guān)閉的實(shí)現(xiàn)方法
用過ios的都知道ios上輸入法關(guān)閉的同時(shí)會(huì)自動(dòng)關(guān)閉輸入框,那么在android上如何實(shí)現(xiàn)監(jiān)聽輸入法彈出和關(guān)閉呢?接下來通過本文給大家分享一種可靠的實(shí)現(xiàn)方式2016-11-11
Flutter 枚舉值enum和int互相轉(zhuǎn)化總結(jié)
這篇文章主要為大家介紹了Flutter 枚舉值enum和int互相轉(zhuǎn)化總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

