如何将可绘制文件夹中的图像添加到Android Studio 1.2中的ImageView?

时间:2015-05-10 18:18:35

标签: android image imageview drawable

由于Google为启动器图标添加了mipmap文件夹,因此我在使用drawables文件夹时遇到了问题。我在可绘制文件夹中手动添加了hdpi文件夹,但是当我尝试添加src路径时,图像将不允许我查看和选择它们。如何将可绘制文件夹中的图像添加到Android Studio 1.2中的ImageView?

5 个答案:

答案 0 :(得分:5)

尝试使用这种代码方式,有三种方法可以在ImageView上设置图像

imageView.setImageResource(R.drawable.play);

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

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

答案 1 :(得分:1)

将PNG图像粘贴到以下目录<Project-Name>\app\src\main\res\drawable\中 (如果drawable目录不存在,请创建一个新目录。)

现在像我完成的那样将src属性添加到imageView。

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

                android:src="@drawable/image_name"

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

其中image_name是您刚刚粘贴到drawable目录中的PNG图像的名称。

如果要将图像粘贴到hdpi部分,则必须在res\文件夹下创建目录,然后路径为<Project-Name>\app\src\main\res\drawable-hdpi\

答案 2 :(得分:0)

屏幕的左上角,项目名称正下方,有项目概述(目录,文件等),将其从“Android”更改为“项目”。然后转到/ app / src / main / res。然后右键单击res并在里面创建一个drawable文件夹!把你的东西放在那里。您可以使用以下方式访问资源: @drawable / ...在你打算使用它的时候。

答案 3 :(得分:0)

在drawable文件夹中添加hdpi文件夹将无法正常工作,因为它不会被android发现。为了便于导航,您可以从 Android &#39;切换和查看Project Explorer到&#39; 项目&#39;如下所示:

enter image description here

在项目视图中,您可以浏览所有可绘制的文件夹,包括hdpi,mdpi等,如下面的路径src app / src / main / res /...

enter image description here

答案 4 :(得分:0)

事实证明,我需要制作可绘制的hdpi文件夹子文件夹。然后Android Studio会自动将它们归类为drawable文件夹。感谢您的所有帮助!