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