The check box icon in the Custom checkbox, this time because you want to lazy, the icon is bigger, and then a series of problems are cited.
1, the icon is higher than the layout_height of the checkbox, is invisible.
Very vomiting blood, compoundbutton in the source can see the following code
protected void OnDraw (canvas canvas) { super.ondraw (canvas); Final drawable buttondrawable = mbuttondrawable; if (buttondrawable! = null) { final int verticalgravity = getgravity () & Gravity.vertical_gravity_mask; Final int height = buttondrawable.getintrinsicheight ();//You can see that the picture is drawn according to the original height of the picture, danielinbiti int y = 0; Switch (verticalgravity) {case gravity.bottom: y = getheight ()-height; break; Case gravity.center_vertical: y = (getheight ()-height)/2; break; } Buttondrawable.setbounds (0, Y, buttondrawable.getintrinsicwidth (), y + height); Buttondrawable.draw (canvas);} }
From here you can see that it is drawn according to the height of the image, not the layout_height you set for picture scaling. So there are two ways to solve it:
The first method: reduce the original image, this is the production of pictures
Second method: Overriding the OnDraw method
@Override protected void OnDraw (canvas canvas) {setbuttondrawable (Parentds);//danielinbiti,<span style= "Font -family:arial, Helvetica, Sans-serif; >parentds is </span><span style= "font-family:arial, Helvetica, Sans-serif;" Returned by the >occupyposdrawable method. </span><span style= "font-family:arial, Helvetica, Sans-serif;" ></span> Super.ondraw (canvas); Setbuttondrawable (DS);//The real picture to be displayed final drawable buttondrawable = ds; if (buttondrawable! = null) {final int verticalgravity = getgravity () & Gravity.vertical_gravity_mask; Final int drawableheight = Buttondrawable.getintrinsicheight () >selfheight? selfheight:buttondrawabl E.getintrinsicheight (); Final int drawablewidth = Buttondrawable.getintrinsicwidth () >selfheight? SelfHeight:buttonDrawable.getIntrin Sicwidth (); <span style= "font-family:arial, Helvetica, Sans-serif;" >selfheight is the height of the picture to be defined </SPAN> int top = 0; Switch (verticalgravity) {Case Gravity.BOTTOM:top = getheight ()-drawableheight; Break Case Gravity.CENTER_VERTICAL:top = (getheight ()-drawableheight)/2; Break } int left = 0; int right = Left+drawablewidth; int bottom = Top+drawableheight; Buttondrawable.setbounds (left, top, right, bottom); Buttondrawable.draw (canvas); }}//drawable converted to bitmap private drawable occupyposdrawable (int height) {//Picture p=new picture (); Canvas c=p.beginrecording (height,height); P.endrecording (); Picturedrawable pd=new picturedrawable (p); return PD; }
2. After this settlement, after thinking that it has been completed, the result defines the paddingleft, the problem arises, in the 4.1.2 version is not normal (picture and text stacked together), in 4.2.2 version of the normal display paddingleft
This really took a lot of time, and found that 4.1.2 version of Compoundbutton No getcompoundpaddingleft, this is not deceptive. Two versions are calculated differently, 4.2.2 when the text is drawn, the width of the image and the width of the paddingleft are added, and the 4.1.2 version only calculates the paddingleft of the setting, which is also 4.2.2 version of the user-friendly.
Then checked the back and forth version, found that 4.1.2 is the dividing line, and subsequent versions of Compoundbutton have getcompoundpaddingleft methods. And then rewrite the Getcompoundpaddingleft method, judging by the version
@Override public int Getcompoundpaddingleft () { int padding = Super.getcompoundpaddingleft (); if (Getandroidsdkversion () <=16) {//4.1.2 if (!islayoutrtl ()) { padding = selfheight + padding; } } return padding; }
Here, the basic definition of a checkbox. The size of the picture does not have to be too concerned about, you can set the size of the image to zoom. It's relatively flexible in app applications, and it also finds an Android version difference problem. Before using a checkbox basically did not set the paddingleft, really did not pay attention to this problem (because not set two versions are normal, just paste this picture)
Problems with padding in the checkbox in Android