Objective
TextView's Drawableleft, Drawableright, and drawabletop are a common, easy-to-use attribute that can be placed around the top and bottom of the text without using a more complex layout. I also often like to use the RadioButton of these several properties to achieve a lot of results, but do not support to let drawbleleft with the text center, set gravity for center also useless, finally have the time to study, here with you to share.
One, the effect chart
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/
Second, the implementation of the Code
Custom controls
/** * Drawableleft centered with text * * @author farmer Uncle * @see http://www.cnblogs.com/over140/p/3464348.html * * * Public CLA
SS Drawablecentertextview extends TextView {public Drawablecentertextview-context, AttributeSet attrs,
int defstyle) {Super (context, attrs, Defstyle);
Public Drawablecentertextview (context, AttributeSet attrs) {Super (context, attrs);
Public Drawablecentertextview {Super (context);
} @Override protected void OnDraw (Canvas Canvas) {drawable[] drawables = Getcompounddrawables ();
if (drawables!= null) {drawable drawableleft = drawables[0];
if (drawableleft!= null) {Float textWidth = Getpaint (). Measuretext (GetText (). toString ());
int drawablepadding = getcompounddrawablepadding ();
int drawablewidth = 0; Drawablewidth = Drawableleft.getintrInsicwidth ();
Float bodywidth = textWidth + drawablewidth + drawablepadding;
Canvas.translate ((GetWidth ()-bodywidth)/2, 0);
} super.ondraw (canvas); }
}
Consistent with normal TextView usage, no additional attributes are added.
2013-12-13 Note, Drawableright doesn't work! Thanks netizens to remind, the follow-up has the progress to update the article again.
End
The technical problems that make you miserable must find some time to figure it out. ta!
Author: cnblogs Peasant Uncle