Android sets the background image xml's background and java's getDrawable ()

Source: Internet
Author: User
Tags set background

How to Set background images

Xml file settings in the background image:

For any widget, button imageView or layout, add


[Java] android: background = "@ drawable/bg"

 

You can set the background image to bg.

Bg. bnp Paribas are stored in the drawable directory.

The drawable directory stores images shared by the size icons. Drawable-hdpi stores 240 WVGA800 simulators or board pictures. Drawable-mdpi stores the corresponding small image

 


[Java] <ImageView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/pic"/>

In the same example, you can set the image as pic.png using src.pdf.

 


--------------------------------------------------------------------------------

 

Set or change the background image of a widget in java code:

 


[Java] Resources resources = getContext (). getResources ();
Drawable btnDrawable = resources. getDrawable (R. drawable. layout_bg );
Layout. setBackgroundDrawable (btnDrawable );


 

In the program, the background image of layout is dynamically modified and implemented using the setBackgroundDrawable () function. Set the background image to layout_bg.png.

 

Modify the background image of imageView1 to the background image of imageView2:

ImageView2.getDrawable () can obtain the background of Image 2. The value is of the Drawable type.

ImageView1.setImageDrawable (drawable); sets the background image of imageView1.

 

That is:


[Java] imageView1.setImageDrawable (imageView2.getDrawable ());

 


Translated by Zhang

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.