【问题标题】:Adding the same image every time a button is clicked每次单击按钮时添加相同的图像
【发布时间】:2017-12-08 09:17:21
【问题描述】:

我为教师制作了一个绘图板,我想在其中包含几何形状,点击后会显示一个形状的按钮。 我现在正在做的是我已经创建了一个图像视图并将其可见性设置为消失,在按钮上我使其可见。

<ImageView
        android:id="@+id/ivReact"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@drawable/rectangle"
        android:visibility="gone"
        android:layout_centerInParent="true"

        />

我希望之前的图像在那里,当单击按钮时,应在布局中添加两次相同的图像,如果再次单击按钮,则应再次添加并继续。
有可能吗?

【问题讨论】:

  • 创建图像数组并通过单击按钮替换 imageview 中的图像
  • 它将替换图像,但我想再次添加它..
  • 你想把图片加入列表吗?
  • 不在列表中.. 我有一个布局,我在其中添加图像,就像一个圆圈的图像.. 我想在单击按钮时在同一布局中添加更多圆圈..
  • 重叠的图像是你想说的吗?

标签: java android imageview


【解决方案1】:
 Use this code inside onclick 

ImageView imageview = new ImageView(MainActivity.this);
     RelativeLayout relativelayout = (RelativeLayout)findViewById(R.id.relativeLayout);
     LinearLayout.LayoutParams params = new LinearLayout
     .LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

     // Add image path from drawable folder.
     imageview.setImageResource(R.drawable.demo_new_image);
     imageview.setLayoutParams(params); 
     relativelayout.addView(imageview); 

【讨论】:

    【解决方案2】:

    创建一个回收站视图并将图像视图放在那里。创建一个数组列表。 Onclick 将相同的图像添加到数组列表并应用 adapter.notifyDataSetChanged()。

    【讨论】:

      【解决方案3】:

      您必须在单击按钮时以编程方式更新图像:

      imageView.setImageResource(R.drawable.your_image);
      

      编辑:

      如果你在 LinearLayout 中有 ImageViews,你可以简单地新建并添加到布局中:

      ImageView imageView = (ImageView) LayoutInflater.from(context).inflate(R.layout.image_view_layout_name, null);
      linearLayout.addView(imageView);
      

      【讨论】:

      • 不,我希望之前的图像在那里,当单击按钮时,应在布局中添加两次相同的图像,如果再次单击按钮,则应添加三次并继续..
      • @ManojKumar 然后在运行时单击时添加视图。
      • @ManojKumar 我已经更新了我的答案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 2019-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      相关资源
      最近更新 更多