How to add borders to text by using Android notes

Source: Internet
Author: User

Bordertextviews. Java

Package xiaosi. bordertextview; </P> <p> Import android. content. context; <br/> Import android. graphics. canvas; <br/> Import android. graphics. color; <br/> Import android. graphics. paint; <br/> Import android. util. attributeset; <br/> Import android. widget. textview; </P> <p> public class bordertextviews extends textview <br/> {<br/> private paint = NULL; <br/> private int color = color. gray; <br/> Public bordertextviews (context, attributeset attrs) <br/>{< br/> super (context, attrs ); <br/>}< br/> // set the border color <br/> Public void setpaintcolor (INT color) {<br/> This. color = color; <br/>}< br/> @ override <br/> protected void ondraw (canvas) <br/>{< br/> super. ondraw (canvas); <br/> paint = new paint (); <br/> // set the border color <br/> paint. setcolor (color); <br/> // upper <br/> canvas. drawline (0, 0, this. getwidth ()-1, 0, paint); <br/> // left <br/> canvas. drawline (0, 0, 0, this. getheight ()-1, paint); <br/> // <br/> canvas. drawline (0, this. getheight ()-1, this. getwidth ()-1, this. getheight ()-1, paint); <br/> // right <br/> canvas. drawline (this. getwidth ()-1, 0, this. getwidth ()-1, this. getheight ()-1, paint); <br/>}< br/>}

 

Package xiaosi. bordertextview; </P> <p> Import android. app. activity; <br/> Import android. graphics. color; <br/> Import android. OS. bundle; </P> <p> public class bordertextviewactivity extends activity {<br/>/** called when the activity is first created. */<br/> private bordertextviews bordertextview = NULL; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> bordertextview = (bordertextviews) findviewbyid (R. id. border); <br/> bordertextview. setpaintcolor (color. gray); <br/>}< br/>}

 

Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: Orientation = "vertical" <br/> Android: Background = "# ccff66"> </P> <p> <xiaosi. bordertextview. bordertextviews <br/> Android: Id = "@ + ID/border" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: textcolor = "# c71585" <br/> Android: layout_margintop = "20dp" <br/> Android: padding = "10dp" <br/> Android: layout_gravity = "center" <br/> Android: TEXT = "draw borders on the canvas"/> </P> <p> </linearlayout>

 

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.