W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
Android生成放置在/res/drawable
子目錄中的圖像文件的資源ID。
支持的圖像類型包括.gif,.jpg和.png。此目錄中的每個(gè)圖像文件都從其基本文件名生成唯一的ID。
如果圖像文件名為 sample_image.jpg
,那么生成的資源ID是 R.drawable.sample_image
。
對于具有相同基本文件名的兩個(gè)文件名,將出現(xiàn)錯(cuò)誤。將忽略 /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中的圖像,并將其設(shè)置為類似于按鈕的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還支持一種特殊類型的圖像,稱為可拉伸圖像。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: