自訂真正帶圖片的Toast

來源:互聯網
上載者:User

      先上:

再上代碼:

public class ImageToast {/** * @author spare_H * @param  msg   顯示的內容 * @param  resId 顯示的圖片資源的id * @param  which 顯示方式1.添加一個TextView和一個ImageView2.只添加一個TextView */public static void ShowToastwithImage(Context context, String msg,int resId, int which) {Toast toast = new Toast(context);TextView mTextView = new TextView(context);// 設定“背景布局”LinearLayout mLinearLayout = new LinearLayout(context);mLinearLayout.setBackgroundResource(R.drawable.toast_frame);//toast_frame就是系統的toast的背景圖片if (which == 1) {mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);ImageView mImageView = new ImageView(context);mImageView.setImageResource(resId);mLinearLayout.addView(mImageView);mTextView.setText("\n"+msg);//換行為了類比文字置中} else if (which == 2) {mTextView.setText(msg);mTextView.setGravity(Gravity.CENTER_VERTICAL);//設定文字置中mTextView.setCompoundDrawablesWithIntrinsicBounds(resId, 0, 0, 0);}mLinearLayout.addView(mTextView);toast.setView(mLinearLayout);toast.setDuration(Toast.LENGTH_LONG);toast.show();}}

              方式一有點糊弄人的感覺,文字置中效果是假的;方式二比較不錯,一個Textview搞定。如果是在Service中調用的話,可以用Context mContext = (Context) getBaseContext();得到context。

系統Toast的布局檔案如下(transient_notification.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/toast_layout_root"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:background="@drawable/toast_frame">    <TextView        android:id="@android:id/message"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:textAppearance="@style/TextAppearance.Small"        android:textColor="@color/bright_foreground_dark"        android:shadowColor="#BB000000"        android:shadowRadius="2.75"        /></LinearLayout>

看到這個,估計就都明白Toast的布局可以很容易自訂了。

      關於控制Toast顯示時間,網上一位兄弟給出了方法,就是使用Timer和TimerTask

 ,未能找到原始出處,代碼如下:

private Toast toast = null;private class IntroButtonListener implements OnClickListener {        @Override         public void onClick(View v) {         // TODO Auto-generated method stub         toast = Toast.makeText(StartPage.this, toastText,Toast.LENGTH_LONG);            initToast();         execToast();        }}private void execToast(){        Timer timer = new Timer();        timer.schedule(new TimerTask(){         @Override         public void run() {               // TODO Auto-generated method stub               initToast();         }           }, 30);}private void initToast(){        toast.show();}

文中解釋如下:

      在這裡將Toast的樣本化延遲到了觸發事件是再響應,然後調用的是makeText()方法,給它設定Duration的值為Toast.LENGTH_LONG,然後將toast.show()用一個方法initToast()包起來,這麼做的目的是讓它顯示在Timer中也會接著顯示Toast。

      這麼做的邏輯是,首先先顯示Toast,然後讓Timer協助再次顯示Toast,這樣就會出現了Toast長時間顯示的效果,如果想讓時間變得更長,可以修改Timer裡面timer.schedule()的值,這裡設定的是30。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.