【问题标题】:How do I add an image in the drawable folder to an ImageView in Android Studio 1.2?如何将可绘制文件夹中的图像添加到 Android Studio 1.2 中的 ImageView?
【发布时间】:2016-10-14 12:26:05
【问题描述】:

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

【问题讨论】:

  • 重启android eclipse或者清理项目然后试试我的代码回答

标签: android image imageview drawable


【解决方案1】:

试试这种代码方式,有三种方法可以在 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" />

【讨论】:

    【解决方案2】:

    将PNG图片粘贴到以下目录&lt;Project-Name&gt;\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 是您刚刚粘贴到可绘制目录中的 PNG 图像的名称。

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

    【讨论】:

    • 它给了我一个错误,说它Couldn't resolve resource @drawable/main_title
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      在drawable文件夹中添加hdpi文件夹是行不通的,因为它不会被android发现。为了更轻松地导航,您可以将 Project Explorer 从“Android”切换和查看到“Project”,如下所示:

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

      :

      【讨论】:

      • 我已经有了这些文件夹,但我无法访问其中的内容。
      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-16
        • 2016-05-31
        • 1970-01-01
        • 2012-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多