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