4. Dynamic configuration of Drawabletop (Drawableboottom, Drawableleft, drawableright) in Java code in Android XML

Source: Internet
Author: User

As a friend of Android developers know, we can use this to set the top of the button or (bottom, left, right) picture resources in XML:

So what if you need to configure the picture dynamically? We have to use Java code to do this:

Drawable drawable =NULL;d rawable=splash.contexttools.getresources (). getdrawable (R.drawable.pepper);//Get Drawable ObjectDrawable.setbounds (0, 0, 24,75);//set the width and height of a drawable objectNew_button.setcompounddrawables (NULL,NULL, Drawable,NULL);//put the Drawable object on the right side of the New_button (here is a note, if you use the Setcompunddrawables method, you must first configure a resource through SetBounds)new_button.setcompounddrawablepadding (35);//set the spacing between drawable and New_button

Through the above code, we basically meet the needs of our use.

Some students will ask, if I want to ensure that the original size of the picture what to do?

Setcompounddrawableswithintrinsicbounds (null,null,drawable,null);//This method can use the intrinsic width and height of the picture

OK, next time we share the other

4. Dynamic configuration of Drawabletop (Drawableboottom, Drawableleft, drawableright) in Java code in Android XML

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.