【问题标题】:Cannot show image in toast无法在吐司中显示图像
【发布时间】:2011-08-03 17:28:52
【问题描述】:

我想在 Android 的 Toast 中显示图像。在我的 layout.xml 中,我定义了一个 LinearLayout 'svllid',其中包含一个 textview 和一个 tablelayout,如下所示:

<?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>

我想在 Toast 中显示 ID 为 'svllid' 的 LinearLayout,并显示我的活动代码中的 toast。

现在,在实际的android代码中,我首先调用

setContentView(R.layout.main); 

随后,我从一条肥皂信息中读取了一张图片。然后我创建一个 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

有什么想法吗?

【问题讨论】:

  • 我猜你不能这样做。您的 svllid 已在活动中。您无法再次将其添加到 toast 中。可能是您需要以编程方式创建一个新视图或 inflate 另一个布局,然后 setView() 用于 toast。
  • 那么我应该从 main.xml 中取出 'svllid' 并以编程方式创建它吗?
  • 是的。将其保存在单独的 xml 中。使用LayoutInflator 膨胀新的 xml,然后将其设置为 toast。或者以编程方式创建它并将其设置为 toast。

标签: android image toast illegalargumentexception


【解决方案1】:

我相信您必须为根布局使用特定的 ID。您必须将其命名为“toast_layout”或“toast_layout_root”(不确定必须使用哪一个 - 那里的文档有点模棱两可,请尝试两者)。

正如 Varun 所说,您需要将布局放在自己的布局文件中。

阅读Android Doc's and follow the given example.

【讨论】:

    【解决方案2】:

    为此,您可以使用自定义 Toast 来显示图像。

    请参考This

    【讨论】:

      猜你喜欢
      • 2016-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 2019-10-17
      相关资源
      最近更新 更多