如何在Android Studio 1.2中将drawable文件夹中的图片添加到ImageView?

5

自从Google为启动器图标添加了mipmap文件夹后,我在使用drawable文件夹时遇到了问题。我手动将hdpi文件夹添加到了drawable文件夹中,但当我尝试添加src路径时,这些图片无法被查看和选择。我该如何将drawable文件夹中的图像添加到Android Studio 1.2中的ImageView中?


重启Android Eclipse或清理项目,然后尝试我的代码答案。 - Amitsharma
5个回答

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
请将PNG图片粘贴到以下目录中:<项目名称>\app\src\main\res\drawable\(如果不存在drawable目录,请创建一个新的)。
现在像我所做的那样,向imageView添加src属性。
           <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\


它给我一个错误,说它“无法解析资源@drawable/main_title”。 - Justin M.

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

0

在drawable文件夹内添加hdpi文件夹是行不通的,因为Android无法发现它。为了更方便地导航,您可以将Project Explorer从“Android”切换到“Project”,如下所示:

enter image description here

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

enter image description here:


我已经有文件夹了,但我无法访问它们里面的内容。 - Justin M.

0
原来我需要不要将可绘制的hdpi文件夹作为子文件夹。然后,Android Studio会自动将它们归类到drawable文件夹下。感谢你们的帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接