Android advanced tutorial (3)-android custom view application.

Source: Internet
Author: User

Hello everyone, our tutorial is onAndroid
Custom in the tutorialView
For beginners, they are usedAndroid
The traditional page layout method is as follows:

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/Hello" <br/> </linearlayout>

 

Of course, the layout method above can help us develop simple applications, but if you want to write a complex application, it will be a little far-fetched. If you don't believe it, you can study the source code, the layout of high handwriting is usually written as follows:

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <A> <br/> <B> </B> <br/> </a>

A extends linerlayout and B extends textview.

 

I wrote a simpleDemo
The procedure is as follows:

 

First createAndroid
Project name:Viewdemo
.

 

Then customizeView
Class, namedMyview (extends view)
The Code is as follows:

 

Package COM. android. tutor; <br/> Import android. content. context; <br/> Import android. graphics. canvas; <br/> Import android. graphics. color; <br/> Import android. graphics. paint; <br/> Import android. graphics. rect; <br/> Import android. graphics. paint. style; <br/> Import android. util. attributeset; <br/> Import android. view. view; <br/> public class myview extends view {<br/> private paint mpaint; <br/> private context mcontext; <br/> Private Static final string mstring = "welcome to Mr Wei's blog"; </P> <p> Public myview (context) {<br/> super (context); </P> <p >}< br/> Public myview (context, attributeset ATTR) <br/>{< br/> super (context, ATTR ); </P> <p >}< br/> @ override <br/> protected void ondraw (canvas) {<br/> // todo auto-generated method stub <br/> super. ondraw (canvas); </P> <p> mpaint = new paint (); </P> <p> // set the paint brush color <br/> mpaint. setcolor (color. red); <br/> // set filling <br/> mpaint. setstyle (style. fill); </P> <p> // draw a rectangle. The first two are the coordinates in the upper left corner of the rectangle, and the last two are the coordinates in the lower right corner. <br/> canvas. drawrect (New rect (10, 10,100,100), mpaint); </P> <p> mpaint. setcolor (color. blue); <br/> // draw text <br/> canvas. drawtext (mstring, 10,110, mpaint); <br/>}< br/>

 

ThenView
AddMain. xml
In the layout file, the Code is as follows:

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/Hello" <br/> <COM. android. tutor. myview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> </linearlayout>

The final execution result is as follows:

 

 

OK, it's all done. I wrote it here today and started cooking. My wife and children waited for me to cook, lol ~

 

 

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.