Android advanced tutorial (3) ---- Android custom View application

Source: Internet
Author: User

Hello everyone, our tutorial is to customize the View in the Android tutorial. For Beginners, they are used to the traditional page layout method of Android, as shown in the following code:

View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
</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:

View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<A>
<B> </B>
</A>

View plaincopy to clipboardprint?
A extends LinerLayout and B extends TextView.

To help you better understand it, I wrote a simple Demo. The specific steps are as follows:

First, create an Android project named ViewDemo.

Then customize a View class named MyView (extends View). The Code is as follows:

View plaincopy to clipboardprint?
Package com. android. tutor;
Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. Rect;
Import android. graphics. Paint. Style;
Import android. util. AttributeSet;
Import android. view. View;
Public class MyView extends View {
Private Paint mPaint;
Private Context mContext;
Private static final String mString = "Welcome to Mr Weis blog ";

Public MyView (Context context ){
Super (context );

}
Public MyView (Context context, AttributeSet attr)
{
Super (context, attr );

}
@ Override
Protected void onDraw (Canvas canvas ){
// TODO Auto-generated method stub
Super. onDraw (canvas );

MPaint = new Paint ();

// Set the paint brush color
MPaint. setColor (Color. RED );
// Set Filling
MPaint. setStyle (Style. FILL );

// 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.
Canvas. drawRect (new Rect (10, 10,100,100), mPaint );

MPaint. setColor (Color. BLUE );
// Draw text
Canvas. drawText (mString, 10,110, mPaint );
}
}

Add the custom View to the main. xml layout file. The Code is as follows:

View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
<Com. android. tutor. MyView
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
/>
</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.