Two ways Android uses code to set radiobutton different orientation icons

Source: Internet
Author: User
Tags null null

Replacing pictures in RadioButton is well-set in XML, but it's not easy to find out how beginners are set up in code. No, by looking at the original API to find two methods, Setcompounddrawables and Setcompounddrawableswithintrinsicbounds.

Here's how you do it.

First method:setcompounddrawableswithintrinsicbounds (drawable left, drawable top, drawable right, drawable bottom)

The original API is:

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use NULL if the want a drawable there. The Drawables ' bounds is set to their intrinsic bounds.

This means that you can set the icon in the top, bottom, left, and right settings, or null if you don't want to show it somewhere. The width height of the graph is set to the intrinsic width, which is obtained automatically via Getintrinsicwidth and Getintrinsicheight. --Author translation

button = (RadioButton) Group.getchildat (i); Resources res = tabtest.  This . Getresources (); drawable myimage = res.getdrawable (r.drawable.home); Button.setcompounddrawableswithintrinsicbounds (nullnull null);

Second method:setcompounddrawables (drawable left, drawable top, drawable right, drawable bottom)

The original API is:

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use NULL if the want a drawable there. The drawables must already has had setBounds(Rect) called.

This means that you can set the icon in the top, bottom, left, and right settings, or null if you don't want to show it somewhere. But drawable must already be setbounds (Rect). It means that the resources you want to add must have been set to the initial position, width and high information. --Author translation

This is understood, this method must first give drawable set SetBounds (x,y,width,height);

X: The start of the component on the x-axis of the container y: The start of the component on the y-axis of the container width: the length of the component height: the high of the component.

such as code:

     Resources res = tabtest.  This . Getresources ();     drawable myimage = res.getdrawable (r.drawable.home);     Myimage.setbounds (11);   button.setcompounddrawables (nullnull null);

Just adjust the width and height. The effect is the same. The advantage of this method is not proportional, wide high can break the original size and proportions ! , the y axis of my adjustment is a bit misaligned.

Summary: RadioButton set different orientation of the icon method has the above two, if you want to manually set the size of the setcompounddrawables, in advance to give drawable set SetBounds.

Use setcompounddrawableswithintrinsicbounds If the picture is displayed according to the original scale size

Two ways Android uses code to set radiobutton different orientation icons

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.