2015-03-24 20 views
13

我在我的佈局中有一個ImageView,請參閱下面的代碼。當我嘗試設置src屬性是我的啓動器圖標,我得到以下錯誤:無法解析符號@ drawable/ic_launcher

Cannot resolve symbol '@drawable/ic_launcher' 

ic_launcer.png添加到我的文件夾可繪製,見下圖。爲什麼我不能在我的ImageView中引用我的圖像?

項目結構:

Project structure

Layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

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

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

回答

28

變化(你有沒有繪製文件夾):

@drawable/ic_launcher 

要(您使用的紋理貼圖文件夾):

@mipmap/ic_launcher 

的貼圖 - 僅用於圖標 - mipmap vs drawable folders

可繪製 - 用於除圖標

其他圖像。根據這個Google blogpost

It’s best practice to place your app icons in mipmap- folders (not the drawable- folders) because they are used at resolutions different from the device’s current density.

+1

感謝您解釋'drawable'和'mipmap'文件夾之間的區別。 – Marcus 2015-03-24 21:41:28

4

drawable都在裏面mipmap,應使用應用程序,啓動程序。如果你想用它作爲繪製你應該複製這些內drawable/一個文件夾

+0

我看到現在,必須已經改變,當我更新的Android工作室。有沒有簡單的方法將它們複製到我的(剛剛創建的)可繪製文件夾中?在AS中,'ctrl + c'和'ctrl + v'似乎不起作用。我必須使用文件資源管理器來完成嗎? – Marcus 2015-03-24 21:29:19

+1

它適用於Mac。如果你不在窗戶上,使用外殼..這是最快捷的方式 – Blackbelt 2015-03-24 21:30:35

+0

@Marcus,[shift] + [f6]快速重命名文件夾。 Mipmap是繪製drawable的新方法。 – 2015-03-24 21:32:14