2 回答

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊
教你如何設(shè)置背景圖片
xml文件設(shè)置背景圖片中:
任意一個(gè)控件,button imageView 或layout,在其的xml屬性設(shè)置中,添加
[java] view plaincopy
android:background="@drawable/bg"
即可實(shí)現(xiàn)設(shè)置其背景圖片為bg.
其中bg.bnp圖片存放在drawable目錄下。
drawable目錄下存放大小圖標(biāo)共用的圖片。drawable-hdpi中存放240 WVGA800 的模擬器或板子用的圖片。drawable-mdpi存放對應(yīng)的小圖片
[java] view plaincopy
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pic"/>
同樣,用src屬性,也可設(shè)置imageView的圖片為pic.png
java代碼中設(shè)置或更改一個(gè)控件的背景圖片:
[java] view plaincopy
Resources resources = getContext().getResources();
Drawable btnDrawable = resources.getDrawable(R.drawable.layout_bg);
layout.setBackgroundDrawable(btnDrawable);
程序中,動(dòng)態(tài)修改layout的背景圖片,用setBackgroundDrawable()函數(shù)實(shí)現(xiàn)。設(shè)置其背景圖片為 layout_bg.png
修改imageView1的背景圖片為imageView2的背景圖片:
imageView2.getDrawable()可獲得圖片2的背景。值為Drawable類型
imageView1.setImageDrawable(drawable); 設(shè)置imageView1的背景圖片
即:
[java] view plaincopy
imageView1.setImageDrawable(imageView2.getDrawable());

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊
代碼如下:
LinearLayout?temp=(LinearLayout)findViewById(R.id.LinearLayout的id);
Drawable?d=Drawable.createFromPath(圖片路徑);
temp.setBackgroundDrawable(d);
- 2 回答
- 0 關(guān)注
- 122 瀏覽
添加回答
舉報(bào)