如何在android中动态添加2个图像视图

时间:2017-11-13 07:07:07

标签: android imageview

我正在开发Android应用程序,其中我想在布局中添加2个imagview。流程在布局中有一个imageview添加用于通过相机拍摄照片当用户从相机拍照时我从相机获取图像我将在该imageview中显示它,并且imageview在它的右上角有一个交叉ImageButton,当图像被加载时它将变得可见,并且当用户点击照片时我想要添加的另一个图像视图会发生同样的事情,但我不知道如何添加这是动态的。我搜索答案,但布局中只添加了一个imageview。请帮我解决这个问题。

我希望这样 as show in this image the imageview has cross button on it

根据Vijendra给出的解决方案,我更新了我的xml和java代码,但有 错误

  

java.lang.IllegalArgumentException:无法将空子视图添加到ViewGroup

这是我的main.xml

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/bicycle_broken_layout">

    <EditText
        android:id="@+id/et_moreinfo_broken_bike"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginTop="15dp"
        android:hint="@string/more_info"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:background="@color/ch_bg"
        android:paddingTop="5dp"
        android:lines="3"
        android:gravity="top|left"
        android:inputType="textMultiLine"/>

    <RelativeLayout
        android:id="@+id/broken_image_photos"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_moreinfo_broken_bike"
        android:layout_marginTop="20dp">

    </RelativeLayout>

    <Button
        android:id="@+id/btn_submit_report"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/broken_image_photos"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_centerHorizontal="true"
        android:background="@color/bg_main"
        android:text="@string/submit_button"
        android:textSize="17sp"
        android:textColor="@color/white"
        android:padding="15dp"/>

</RelativeLayout>

这里是mainactivity.java

public class Broken_Report_Activity extends AppCompatActivity implements 
View.OnClickListener {

private EditText broken_bike_number,broken_bike_location,more_info;
private Button barcode_scanner,location;
private Button submit_report;
private CheckBox seat,pedals,breaks,lock,chain,tier;
private Imageview_add_dynamically photo1,close_photo,framlayout;
RelativeLayout broken_image_layout;
private boolean Isphoto1=false,Isphoto2=false,Isphoto3=false;

private static final int CAMERA_REQUEST = 1888;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_broken_report);

    broken_bike_number = (EditText)findViewById(R.id.et_broken_bike_number);
    broken_bike_location = 
    (EditText)findViewById(R.id.et_broken_bike_location);
    more_info = (EditText)findViewById(R.id.et_moreinfo_broken_bike);

    submit_report = (Button)findViewById(R.id.btn_submit_report);

    seat = (CheckBox)findViewById(R.id.ch_seat);
    lock = (CheckBox)findViewById(R.id.ch_lock);
    chain = (CheckBox)findViewById(R.id.ch_chain);
    breaks = (CheckBox)findViewById(R.id.ch_break);
    pedals = (CheckBox)findViewById(R.id.ch_pedals);
    tier = (CheckBox)findViewById(R.id.ch_tire);

    broken_image_layout =
    (RelativeLayout)findViewById(R.id.broken_image_photos);

    broken_image_layout.addView(photo1);
    broken_image_layout.addView(close_photo);

    photo1.setOnClickListener(this);
    close_photo.setOnClickListener(this);

    submit_report.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch (v.getId()){

        case R.id.broken_img1:
            Intent broken_imag_1 = new 
            Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(broken_imag_1,CAMERA_REQUEST);
            break;

        case R.id.broken_img1_close:
            broken_image_layout.removeView(photo1);
            broken_image_layout.removeView(close_photo);
            break;

        case R.id.btn_submit_report:
            break;
    }
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
data) {
    super.onActivityResult(requestCode,resultCode,data);
    if(requestCode == CAMERA_REQUEST && resultCode == RESULT_OK){
        Bitmap photo = (Bitmap)data.getExtras().get("data");
        photo1.getImgPhoto().setImageBitmap(photo);

    }
}

1 个答案:

答案 0 :(得分:1)

在你的xml中

<HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/multipleimages"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            //add your imageview here programatically
        </LinearLayout>

    </HorizontalScrollView>

在Java代码中

  LinearLayout ll = (LinearLayout) findViewById(R.id.multipleimages);
        ImageView imageView1=//your image 1
        ImageView imageView2=//your image 2

        ll.addView(imageView1);
        ll.addView(imageView2);