Android own definition view (i): underlined TextView

Source: Internet
Author: User

Package Com.francis.underlinetextviewtest;import Android.content.context;import android.content.res.Resources; Import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.util.attributeset;import Android.util.log;import Android.util.typedvalue;import android.widget.TextView;/ * * Created by Francis on 14-10-13.    */public class Underlinetextview extends TextView {private final paint Mpaint = new paint ();    private int munderlineheight = 0;    Public Underlinetextview (Context context) {This (context, NULL);    } public Underlinetextview (context context, AttributeSet Attrs) {This (context, attrs, 0);         } public Underlinetextview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);    Init (context, attrs);        } private void init (context context, AttributeSet attrs) {Resources r = getresources (); Munderlineheight = = 6 Munderlineheight = (int) typedvalUe.applydimension (Typedvalue.complex_unit_dip, 2, R.getdisplaymetrics ()); } @Override public void setpadding (int left, int top, int. right, int. bottom) {super.setpadding (left, top, RI    ght, bottom + munderlineheight);        } @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas);        Draw the underline the same color as the text Mpaint.setcolor (Gettextcolors (). Getdefaultcolor ()); Two points on the diagonal.

Canvas.drawrect (0, GetHeight ()-Munderlineheight, GetWidth (), GetHeight (), mpaint); }}

To add a layout file:

<com.francis.underlinetextviewtest.underlinetextview        android:text= "@string/hello_world"        android: Layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:textstyle= "bold"        Android: Textcolor= "#FFDDDDDD"/>


Android own definition view (i): underlined TextView

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.