2011-08-03 44 views
1

Androidのトーストに画像を表示したいと思います。トーストに画像を表示できません

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#CCCCCC" 
    android:weightSum="1" android:id="@+id/llid"> 
    <EditText 
     android:id="@+id/cpuText" 
     android:hint="Enter cpu" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent"> 
    </EditText> 
    <EditText 
     android:id="@+id/ramText" 
     android:hint="Enter ram" 
     android:layout_height="wrap_content"  
     android:layout_width="fill_parent"> 
    </EditText> 
    <EditText 
     android:id="@+id/bandwidthText" 
     android:hint="Enter bandwidth" 
     android:layout_height="wrap_content"  
     android:layout_width="fill_parent"> 
    </EditText> 
    <Button 
     android:id="@+id/imageRequestButton" 
     android:layout_height="wrap_content" 
     android:text="Download" 
     android:layout_width="fill_parent" android:onClick="sendImageRequest"> 
    </Button>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#00FF00" 
     android:weightSum="1" android:id="@+id/svllid"> 
     <TextView android:text="client profile" 
      android:id="@+id/profileName" 
      android:layout_width="fill_parent" 
      android:textStyle="bold" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:background="#000000"> 
     </TextView> 
     <TableLayout 
      android:paddingBottom="3pt" 
      android:background="#0000FF" 
      android:layout_width="fill_parent" 
      xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content"> 
      <TableRow> 
       <TextView 
        android:paddingLeft="3pt" 
        android:paddingTop="3pt" 
        android:text="Image Name" 
        android:layout_width="150px" 
        android:layout_height="wrap_content" 
        android:textColor="#FFFFFF"/> 
       <TextView 
        android:paddingLeft="3pt" 
        android:text="blah.png" 
        android:textColor="#FFFFFF" 
        android:layout_width="315px" 
        android:layout_height="wrap_content" android:id="@+id/imageName"/>   
      </TableRow> 
      <TableRow> 
       <TextView 
        android:paddingLeft="3pt" 
        android:text="Size" 
        android:layout_width="150px" 
        android:layout_height="wrap_content" 
        android:textColor="#FFFFFF"/> 
       <TextView 
        android:paddingLeft="3pt" 
        android:text="155kb" 
        android:textColor="#FFFFFF" 
        android:layout_width="300px" 
        android:layout_height="wrap_content" android:id="@+id/imageSize"/>   
      </TableRow> 
      <TableRow> 
       <TextView 
        android:paddingLeft="3pt" 
        android:text="Dimensions" 
        android:layout_width="150px" 
        android:layout_height="wrap_content" 
        android:textColor="#FFFFFF"/> 
       <TextView 
        android:paddingLeft="3pt" 
        android:text="250 X 150px" 
        android:textColor="#FFFFFF" 
        android:layout_width="300px" 
        android:layout_height="wrap_content" android:id="@+id/imageDimension"/>   
      </TableRow>       
     </TableLayout>           
    </LinearLayout> 
</LinearLayout> 

が、私はID とのLinearLayoutを表示するに 'をsvllid':私のlayout.xmlでは、私はそうのようなのTextViewとtablelayoutが、含まれているのLinearLayout 'svllid' を定義しています私の活動コードから乾杯しトーストを見せてください。

次に、実際のAndroidのコードでは、私が最初に続い

setContentView(R.layout.main); 

を呼び出し、Iは、SOAPメッセージから画像を読み取ります。私はImageViewを作成し、それをLinearLayout 'svllid'に挿入して、Android ToastにLinearLayoutを表示したいと思います。

   Toast imageToast = new Toast(this); 
       LinearLayout toastLayout = (LinearLayout) findViewById(R.id.svllid); 
       toastLayout.addView(image,1); 
       imageToast.setView(toastLayout); 
       imageToast.setDuration(Toast.LENGTH_LONG); 
       imageToast.show(); 

ただし、動作しません。私のアプリケーションはエラーでクラッシュします:

java.lang.IllegalArgumentException: View not attached to window manager 

なぜでしょうか?

+1

私はこのようにはできないと思います。あなたのsvllidはすでに活動中です。もう一度トーストに追加することはできません。新しいビューをプログラムで作成するか、別のレイアウトを 'inflateする '必要がありますし、トーストのために' setView() 'を実行する必要があります。 – Varun

+0

main.xmlから 'svllid'を取り出し、代わりにプログラムで作成する必要がありますか? – Joeblackdev

+1

はい。別のxmlに保管してください。 'LayoutInflator'を使用して新しいxmlを展開し、それをトーストに設定します。またはプログラムで作成し、トーストに設定します。 – Varun

答えて

1

ルートレイアウトに特定のIDを使用する必要があると思います。 「toast_layout」または「toast_layout_root」という名前を付ける必要があります(使用する必要があるかどうかは不明ですが、ドキュメントは少し曖昧ですが、両方を試してみてください)。

そして、Varunはレイアウトを独自のレイアウトファイルに入れる必要があると言いました。

あなたが画像を表示するカスタムトーストを使用することができます。このためAndroid Doc's and follow the given example.

+0

パーフェクト、ありがとうございます – Joeblackdev

-1

をお読みください。

Thisを参照してください。

+0

カスタムトーストを使用すると画像を表示できます。 –

関連する問題