Android生成放置在/res/drawable
子目錄中的圖像文件的資源ID。
支持的圖像類型包括.gif,.jpg和.png。此目錄中的每個圖像文件都從其基本文件名生成唯一的ID。
如果圖像文件名為 sample_image.jpg
,那么生成的資源ID是 R.drawable.sample_image
。
對于具有相同基本文件名的兩個文件名,將出現(xiàn)錯誤。將忽略 /res/drawable
下的子目錄。
以下代碼顯示如何在XML布局定義中使用圖像資源。
<Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Dial" android:background="@drawable/sample_image" />
下面的代碼顯示了如何檢索Java中的圖像,并將其設置為類似于按鈕的UI對象。
BitmapDrawable d = activity.getResources().getDrawable(R.drawable.sample_image); button.setBackgroundDrawable(d); //or you can set the background directly from the Resource Id button.setBackgroundResource(R.drawable.sample_image);
Android還支持一種特殊類型的圖像,稱為可拉伸圖像。
更多建議: