分享Android中Toast的自訂使用_Android

來源:互聯網
上載者:User

1.Toast源碼分析

老規矩,我們先去看Toast的源碼。

Toast有兩種顯示布局方式,一種最常見調用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這個布局檔案代碼是這樣的

<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"><TextViewandroid: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的文字訊息樣式,其實就是修改Toast根布局和message這個TextView。

Toast的另外一種顯示模式就是自訂布局顯示。這個方法不調用Toast.makeText()方法,而是new一個Toast對象,然後調用setView()方法。當然自訂布局就不會載入transient_notification布局了。

2.實現自訂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;}/** * 設定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;}/** * 設定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;}/** * 短時間顯示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;}/** * 短時間顯示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;}/** * 長時間顯示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;}/** * 長時間顯示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時間 * * @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時間 * * @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看上去有些呆板,我自訂了一個名為toast_radius.xml的背景,代碼如下:

<?xml version="1.0" encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!-- 填充的顏色 --><solid android:color="#ffc107" /><!-- android:radius 弧形的半徑 --><corners android:radius="20dip" /></shape>

然後上面設定背景的代碼改成:

toastUtil.Short(MainActivity.this,"自訂message字型顏色和背景").setToastBackground(Color.WHITE,R.drawable.toast_radius).show();


修改了背景的Toast

雖然官方認為Toast和Snackbar都應該是短文本的形式,不能包含表徵圖,但是個人感覺加上表徵圖還是挺好玩的...

向Toast中添加表徵圖可以這樣:

 ImageView toastImage = new ImageView(getApplicationContext()); toastImage.setImageResource(R.mipmap.ic_launcher); toastUtil.Short(MainActivity.this,"向Toast添加了一個ImageView").setToastBackground(Color.WHITE,R.drawable.toast_radius).addView(toastImage,0).show();


添加表徵圖的Toast

如果你想要Toast顯示自訂的布局,可以這樣:

 View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.image,null); new ToastUtil(MainActivity.this,view,Toast.LENGTH_SHORT).show();


自訂布局Toast,我的布局檔案中只有一個預設表徵圖的ImageView

大家都知道,連續觸發Toast的show()方法的時候,Toast就會排著隊連續展示,感覺上不太友好。所以我先判斷了toast是否沒被建立或者是否被添加了額外的view,如果是的話就重建一個toast對象;如果否的話就只修改message文字和顯示時間。


Toast布局修改,不排隊顯示

總結

我這個工具類不是完全體,大家再根據自己項目的具體需求進行修改。以上就是Android中Toast的花式使用的全部內容,感興趣的小夥伴們快快自己動手實踐起來吧。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.