Android custom controls allow TextView Drawableleft to be centered with text

Source: Internet
Author: User

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

Related Article

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.