教你如何設定背景圖片
xml檔案設定背景圖片中:
任意一個控制項,button imageView 或layout,在其的xml屬性設定中,添加
[java] android:background="@drawable/bg"
即可實現設定其背景圖片為bg.
其中bg.bnp圖片存放在drawable目錄下。
drawable目錄下存放大小表徵圖共用的圖片。drawable-hdpi中存放240 WVGA800 的模擬器或板子用的圖片。drawable-mdpi存放對應的小圖片
[java] <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pic"/>
同樣,用src屬性,也可設定imageView的圖片為pic.png
--------------------------------------------------------------------------------
java代碼中設定或更改一個控制項的背景圖片:
[java] Resources resources = getContext().getResources();
Drawable btnDrawable = resources.getDrawable(R.drawable.layout_bg);
layout.setBackgroundDrawable(btnDrawable);
程式中,動態修改layout的背景圖片,用setBackgroundDrawable()函數實現。設定其背景圖片為 layout_bg.png
修改imageView1的背景圖片為imageView2的背景圖片:
imageView2.getDrawable()可獲得圖片2的背景。值為Drawable類型
imageView1.setImageDrawable(drawable); 設定imageView1的背景圖片
即:
[java] imageView1.setImageDrawable(imageView2.getDrawable());
作者 張譯成