Now our mobile phone basically will have a notepad, use up is also convenient, notepad this stuff, if I want to achieve, how to do? Today we customize a notepad by customizing the view. OK, don't say a lot of nonsense, first to see.
The entire page is still very simple.
1. Customize the View category
OK, so before the text starts, I'd like to start by saying custom view categories, custom view we are divided into three categories
1. Self-drawing controls
The self-Drawing control is our custom view that inherits from the controls, and then expands its functionality, before two custom view blogs (Android Custom view of the birth of the clock, Android custom view of the imitation Address Book side bar sliding, to achieve a-Z letter search) belong to this one.
2. Inheriting from existing controls extending its functionality
Sometimes, we can also inherit from an existing control and then extend its functionality, such as the notepad that we are going to say right now, that is, inherit from EditText, and then extend its function
3. Combining controls
is to put some existing controls together to form a new control, which can refer to my previous blog Android custom UI template graphic detailed
2. Realization of Ideas
OK, finish the classification of the custom view, let's take a look at the implementation of the Notepad to be introduced in this article. First of all, this kind of notebook background is actually a picture, but this one line is I through the canvas DrawLine this API to draw up. When I click on the carriage return, I will go on to draw a new horizontal line. This whole idea is also very simple.
3. Code implementation
First I'll create a class that inherits from EditText, and then I'll declare a few variables, as follows:
[Java]View Plaincopy print?
- line width of horizontal lines
- Private int linewidth = 1;
- The color of the horizontal line
- Private int linecolor = Color.Black;
- Line spacing
- Private int spacing_line = 10;
- Inner margin
- private int padding = 10;
- Brush
- Private Paint Mpaint;
Then do some initialization operations in the constructor, as follows:
[Java]View Plaincopy print?
- Public NotePad (Context context) {
- This (context, null);
- }
- Public NotePad (context context, AttributeSet attrs) {
- This (context, Attrs, 0);
- }
- Public NotePad (context context, AttributeSet attrs, int defstyleattr) {
- Super (context, attrs, defstyleattr);
- Setfocusableintouchmode (true);
- Setgravity (Gravity.top);
- Setlinespacing (Spacing_line, 1);
- Setpadding (+, padding, 10);
- Mpaint = new Paint ();
- Mpaint.setcolor (LineColor);
- Mpaint.setstrokewidth (linewidth);
- }
Setfocusableintouchmode method can let my Notepad get focus, setgravity let me notepad the default position of the cursor in the upper left corner of the control, the Setlinespacing method is mainly to set the line spacing, Setpadding is to set the padding, the last brush initialization will not have to say more.
OK, after the initialization of the operation, we can then complete the horizontal line drawing, the code is as follows:
[Java]View Plaincopy print?
- @Override
- protected void OnDraw (canvas canvas) {
- Super.ondraw (canvas);
- //Get the height of the screen
- int viewheight = GetHeight ();
- //Get the height of each line of text
- int lineheight = Getlineheight ();
- //Calculate the total number of rows per page
- int pagecount = viewheight/lineheight;
- //Circular Draw horizontal line
- For (int i = 0; i < PageCount; i++) {
- Canvas.drawline (padding, (i + 1) * lineheight, getwidth ()-Padding, (i + 1) * lineheight, Mpaint);
- }
- //Gets the total number of rows in the current text
- int linecount = Getlinecount ();
- The number of lines in the text minus the number of rows per page, the remaining value is the number of rows I want to continue drawing
- int extracount = Linecount-pagecount;
- if (Extracount > 0) {
- For (int i = PageCount; i < LineCount; i++) {
- Canvas.drawline (padding, (i + 1) * lineheight, getwidth ()-Padding, (i + 1) * lineheight, Mpaint);
- }
- }
- }
Please see the comment for the code explanation.
OK, that's so simple. Finally, let's refer to my custom control in the layout file:
[Java]View Plaincopy print?
- <?xml version="1.0" encoding="Utf-8"?>
- <relativelayout
- xmlns:android="Http://schemas.android.com/apk/res/android"
- xmlns:tools="Http://schemas.android.com/tools"
- Android:layout_width="Match_parent"
- android:layout_height="Match_parent"
- tools:context="Org.lenve.notepad.MainActivity" >
- <org.lenve.notepad.notepad
- Android:layout_width="Match_parent"
- android:layout_height="Match_parent"
- android:background="@drawable/background"/>
- </RelativeLayout>
Once you've done this, you'll be able to get your app running.
SOURCE Download http://download.csdn.net/detail/u012702547/9502328
Above.
Android Custom View Notepad out scabbard