2010年4月2日金曜日

Android Toast で画像を表示する

Toast に setView() で View を設定すると、任意の View
を表示することができます。

こんな感じ

  1. ImageView v = new ImageView(this);  
  2. v.setImageResource(R.drawable.addnote_off);  
  3. toast = new Toast(this);  
  4. toast.setDuration(Toast.LENGTH_LONG);  
  5. toast.setView(v);  
  6. toast.show();  


View ならばOKなので、xml でレイアウトを
定義した場合は Infrater を使います。

  1. LayoutInflater inflater = getLayoutInflater();  
  2. View v = inflater.inflate(R.layout.iv, null);  
  3. toast = new Toast(this);  
  4. toast.setDuration(Toast.LENGTH_LONG);  
  5. toast.setView(v);  


  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.   android:orientation="vertical"   
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent" >  
  6.   <ImageView  
  7.     android:layout_width="wrap_content"  
  8.     android:layout_height="wrap_content"  
  9.     android:src="@drawable/addnote_off" />  
  10.   <TextView   
  11.     android:layout_width="fill_parent"  
  12.     android:layout_height="wrap_content"   
  13.     android:text="hello toast"  
  14.     android:textColor="#000000"  
  15.     android:background="#ffffff" />  
  16. </LinearLayout>  

0 件のコメント:

コメントを投稿