【发布时间】: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