How to set image in ImageView?

2019-07-26 21:58发布

问题:

I need to put my own image on some imageView. So that's why I created folder named "drawable" in res folder , and copy my Image in this folder , but now I don't know how to set this image to my imageView in XML code. Can anybody help? and sorry for English :)

回答1:

In your layout XML file:

<ImageView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/your_image_name"/>

Replace your_image_name with the name of the image file without the extension (e.g. if your drawable file is called img1.png, the src attribute should have a value of @drawable/img1).



回答2:

Set it using android:src=""

   <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image_name" />


回答3:

Try this.

<ImageView
    android:id="@+id/YourImageViewID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/Your_Image_Name"
   />

This may help you.



回答4:

//set background as your image by calling the android:background attributes in your xml

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/myimage"
    />

Note: you no need to create any drawable folder by default android project will contain

drawable-mdpi
drawable-hdpi
drawable-ldpi

you can put your image in that.



回答5:

<ImageView
 android:layout_width="150dp"
 android:layout_gravity="bottom"
 android:layout_height="150dp"
 android:id="@+id/image1"
 android:layout_marginTop="11dp"
 android:background="@drawable/image1"
          />