0

在我的布局中,我想在用户单击按钮时在屏幕上添加图像。图像的数量取决于用户,即在运行时决定。布局充气器如何用于此目的,以便我的视图随用户输入动态变化。

4

4 回答 4

0

我认为在您的情况下不需要布局充气器,您可以在 Java 中动态创建图像视图并将其添加到您的主布局中。

LinearLayout ll=(LinearLayout)findViewById(R.id.yourLL);
while(count<yourusersimagesNeeded)
{
ImageView imageView = new ImageView(Activity);
// set src and other attrs
ll.addView(imageView);
count++;
}
于 2013-04-05T03:57:54.257 回答
0

ViewGroup您可以使用该方法动态地将视图添加到 a中addView(View)。请参阅文档:http: //developer.android.com/reference/android/view/ViewGroup.html

大致:

ImageView imageView = new ImageView(Activity);
imageView.setImageResource(R.drawable.my_image); // or setImageBitmap, etc
myRootView.addView(imageView);

新视图的显示位置取决于它添加到的 ViewGroup 的布局类型。有关如何动态定位视图的更多信息,请参阅LayoutParams上的文档。

于 2013-04-05T04:34:20.910 回答
0

假设您的活动布局是一个 LinearLayout “main_layout”,现在您可以使用以下代码动态添加错误的图像视图。

LinearLayout main_container = (LinearLayout) findViewById(R.id.main_layout);
LayoutInflater mInflater;
mInflater = LayoutInflater.from(YourActivity.this);


ImageView image = (ImageView) mInflater.inflate(R.layout.your_image_view, null);
main_container.addView(image);
于 2013-04-05T04:36:15.117 回答
0

为什么需要布局充气机。您只需要在按钮单击时创建图像视图并将其添加到按钮下方或上方的布局(或您要添加的任何位置),如果您将侦听器添加到图像视图,也可以删除添加的图像,即单击图像视图从布局中删除。

于 2013-04-05T04:43:57.467 回答