Android ToDoList (custom style) detailed

Source: Internet
Author: User
Tags getcolor resource

Android allows you to derive subclasses from the existing View toolbox (Widget Tool Box) or implement your own view controls;

By overriding the event handlers and the OnDraw () method, but still recalling the superclass (super) method, you can customize the view without having to have its features solid;

Predecessor steps See: http://blog.csdn.net/caroline_wendy/article/details/21246963

Steps:

1. Create a Todolistitemview class that customizes the appearance of item items:

Location: Java->package->todolistitemview.java

Package Mzx.spike.todolist.app;  
Import Android.content.Context;  
Import android.content.res.Resources;  
Import Android.graphics.Canvas;  
Import Android.graphics.Paint;  
Import Android.util.AttributeSet;  
      
Import Android.widget.TextView; 
 /** * Created by C.l.wang on 14-3-16.  
    * * public class Todolistitemview extends textview{private Paint marginpaint;  
    Private Paint Linepaint;  
    private int papercolor;  
      
    private float margin;  
        Public Todolistitemview (context, attributeset ATS, int ds) {Super (context, ATS, DS);  
    Init ();  
        Public Todolistitemview {Super (context);  
    Init ();  
        Public Todolistitemview (context, AttributeSet ATS) {Super (context, ATS);  
    Init (); 
        private void Init () {//Get a reference to the resource list//See more Highlights in this column: http://www.bianceng.cn/OS/extra/ Resources myresources = Getresources ();  
        Marginpaint = new Paint (Paint.anti_alias_flag);  
      
        Marginpaint.setcolor (Myresources.getcolor (R.color.notepad_margin));  
        Linepaint = new Paint (Paint.anti_alias_flag);  
      
        Linepaint.setcolor (Myresources.getcolor (r.color.notepad_lines));  
        Papercolor = Myresources.getcolor (R.color.notepad_paper);  
    margin = myresources.getdimension (r.dimen.notepad_margin);  
      
        @Override public void OnDraw (Canvas Canvas) {canvas.drawcolor (papercolor);  
        Canvas.drawline (0, 0, 0, getmeasuredheight (), linepaint);  
      
        Canvas.drawline (0, Getmeasuredheight (), Getmeasuredwidth (), Getmeasuredheight (), linepaint);  
      
        Canvas.drawline (margin, 0, margin, getmeasuredheight (), marginpaint);  
        Canvas.save ();  
      
        Canvas.translate (margin, 0);  
        Super.ondraw (canvas);  
    Canvas.restore (); }  
      
}

Detailed

1. Inherits the TextView class, is the text view customization;

2. Overloaded constructors, containing overloaded versions of three parameters, and after callback superclass (super), initialize the resource private variable (init);

3. In init (), obtain a reference to the resource list (getresource), convert the resource file to a callable parameter (MYRESOURCE.GETXXX), and initialize the resource private variable;

4. Rewrite (Override) OnDraw method, set color, draw line, specify write format;

5. Canvas.translate (), so that the output file, the margin distance after moving;

2. Create a color (colors) resource file

Location: Res->values->colors.xml

<?xml version= "1.0" encoding= "Utf-8"?>  
<resources>  
    <color name= "Notepad_paper" ># eef8e0a0</color>  
    <color name= "Notepad_lines" > #FF0000FF </color>  
    <color name= "notepad _margin "> #90FF0000 </color>  
    <color name=" Notepad_text "> #AA0000FF </color>  
</ Resources>

Color resource files, with color tags, Android studio will display colors;

3. Modify the size (dimen) resource file:

Location: Res->values->dimen.xml

<resources>  
    <!--Default screen margins, per the Android guidelines-->
    <dimen name= " Activity_horizontal_margin ">16dp</dimen>  
    <dimen name=" Activity_vertical_margin ">16dp</ dimen>  
    <dimen name= "Notepad_margin" >30dp</dimen>  
</resources>

Supplement can be;

4. Create Todolist_item Layout file:

Location: Res->layout->todolist_item.xml

<?xml version= "1.0" encoding= "Utf-8"?> <mzx.spike.todolist.app.todolistitemview xmlns:android=
    " Http://schemas.android.com/apk/res/android "
    android:orientation=" vertical "
    android:layout_width=" Match_parent "
    android:layout_height=" match_parent "
    android:padding=" 10DP "
    android:scrollbars=" Vertical "
    android:textcolor=" @color/notepad_text "
    android:fadingedge=" vertical "
 />"

Detailed

1. The label is class name, namely Todolistitemview class, overload the method of TextView;

2. Set the corresponding attribute label;

5. Modify the adapter (Adapter) and use the customized TextView:

Location: java->package->todolistactivity

......  
        int resid = R.layout.todolist_item;  
        Three parameter  
        AA = new Arrayadapter<string> (this, resid, todoitems);  
      
        Todolistfragment.setlistadapter (AA);  
......

Detailed

Locate the ID of the resource file, incoming adapter;

6. Executing program: Code download: http://download.csdn.net/detail/u012515223/7050371

Author: csdn Blog spike_king

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.