How do I add an image in the drawable folder to an

2020-07-16 02:42发布

Since Google added the mipmap folder for the launcher icons, I have had issues using the drawables folder. I manually added the hdpi folders in the drawable folder, but when I try and add the src path, the images won't allow me to view and select them. How do I add an image in the drawable folder to an ImageView in Android Studio 1.2?

5条回答
Explosion°爆炸
2楼-- · 2020-07-16 02:54

Top left of the screen, right below the Project name, there's a project overview (directories, files etc.) change it from 'Android' to 'Project'. then go to /app/src/main/res. then right click on res and create a drawable folder inside! Put your thing there. you can access the resource using: @drawable/... inside whenever you're going to use it.

查看更多
闹够了就滚
3楼-- · 2020-07-16 03:04

Adding hdpi folder inside of the drawable folder will not work, because it will not be discovered by android. For easier navigation you can switch and view of Project Explorer from 'Android' to 'Project' like below:

enter image description here

In project view you can explore all the drawable folders including hdpi, mdpi etc like the following path src app/src/main/res/...

enter image description here:

查看更多
别忘想泡老子
4楼-- · 2020-07-16 03:07

try with this way of code there are three ways to set image on ImageView

imageView.setImageResource(R.drawable.play);

or

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

or

              <ImageView
              android:id="@+id/ivProfileBg"
              android:background="@drawable/image_name"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" />
查看更多
倾城 Initia
5楼-- · 2020-07-16 03:07

It turns out that I needed to not make the drawable hdpi folders subfolders. Android Studio then automatically categorizes them under the drawable folder. Thanks for all of your help!

查看更多
The star\"
6楼-- · 2020-07-16 03:14

Paste the PNG image in the following directory <Project-Name>\app\src\main\res\drawable\ (if the drawable directory doesn't exist, create a new one).

now add the src attribute to the imageView like i done.

           <ImageView
                android:id="@+id/ivProfileBg"

                android:src="@drawable/image_name"

                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

where the image_name is the name of the PNG image you just pasted into the drawable directory.

if you wanted to paste the image into the hdpi section, you must create the directory under res\ folder, then the path would be <Project-Name>\app\src\main\res\drawable-hdpi\

查看更多
登录 后发表回答