Android Custom Controls

Source: Internet
Author: User

Custom General Sub-three cases

1. Custom Layouts

2. Custom Controls

3. Direct Inheritance View

Here's the EoE example, implementing a custom control

1. Custom Attributes

Res/values/attrs.xml Custom Properties

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <declare-styleablename= "Progress">        <attrname= "text"format= "string" />        <attrname= "TextSize"format= "Dimension" />    </declare-styleable></Resources>

2. Customizing the control to implement a construction method with AttributeSet

 PackageCom.test.uidemo;ImportAndroid.content.Context;ImportAndroid.content.res.TypedArray;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;ImportAndroid.widget.TextView;ImportCOM.TEST.UIDEMO.R;/*** Refer to EOE Books * Custom controls * Implements wrapping text with a border.*/ Public classMytextviewextendsTextView { PublicMytextview (Context context) {Super(context); }     PublicMytextview (Context context, AttributeSet attrs) {Super(context, attrs); TypedArray attr=context.obtainstyledattributes (Attrs, r.styleable.progress);        SetText (attr.getstring (R.styleable.progress_text)); Settextsize (Attr.getdimension (R.styleable.progress_textsize,16)); LOG.I ("Text", Attr.getstring (R.styleable.progress_text)); }    protected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); Paint Paint=NewPaint ();        Paint.setcolor (Android.graphics.Color.RED); Canvas.drawline (0,0, This. GetWidth () -1,0, paint); Canvas.drawline (0,0,0, This. GetHeight ()-1, paint); Canvas.drawline ( This. GetWidth () -1,0, This. GetWidth ()-1, This. GetHeight ()-1, paint); Canvas.drawline (0, This. GetHeight ()-1, This. GetWidth ()-1, This. GetHeight ()-1, paint); }}

3. Using a custom control

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"xmlns:progress= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.test.uidemo.MainActivity" > <Com.test.uidemo.MyTextView android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Progress:text= "Custom"progress:textsize= "30DP"android:layout_centervertical= "true"Android:layout_alignparentright= "true"Android:layout_alignparentend= "true" > </com.test.uidemo.MyTextView></RelativeLayout>
View Code

Android Custom Controls

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.