android設定背景圖片xml的background和java的getDrawable()

來源:互聯網
上載者:User

教你如何設定背景圖片

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()); 

 


作者 張譯成

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.