Android custom view (Elementary), androidview

Source: Internet
Author: User

Android custom view (Elementary), androidview

Q1: Why is custom view required? A: Because the view provided by many systems cannot meet the current design requirements or to improve user experience and enhance UI beautification, You need to customize viewQ2: what are the steps for customizing a view? A:> You Can extends View the parent class as needed, and then rewrite the methods of the parent class, such as onDraw () and onMeasure ().> if you need to add attributes to a custom View, you must create "attrs. xml file to add custom properties. The following describes how to learn custom views. 1. The most basic custom view. For example, creating a CustomEditText class inherits EditText. All the methods with EditText are identical to EditText, but their names are different. You can use either of the following methods:> call in code, CustomEditText cet = new CustomEditText (context);> to call in xml, you need to write the full-outsourcing name and class name, otherwise, The following classes cocould not be found is reported. The key code is as follows: Custom view: public class CustomEditText extends EditText {public CustomEditText (Context context) {super (context ); // TODO Auto-generated constructor stub} public CustomEditText (Context context, AttributeSet attrs) {super (context, attrs); // TODO Auto-generated constructor s Tub} public medmedittext (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); // use in TODO Auto-generated constructor stub} xml: <! -- Com. anqiansong. views is the package name where CustomEditText is located --> <com. anqiansong. views. customEditText android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Custom EditText"/>

2. Override the onDraw () method in CustomEditText to achieve the strikethrough text effect. @ Overrideprotected void onDraw (Canvas canvas) {// TODO Auto-generated method stubsuper. onDraw (canvas); Paint paint = new Paint (); // creates a paint brush object. setColor (Color. BLACK); // set the paint color to paint. setAntiAlias (true); // you can specify whether the image is anti-sawtooth paint. setStrokeWidth (2); // set the width of the paint brush // set the starting point of the X axis and canvas of the Y axis. drawLine (this. getLeft (), (this. getBottom ()-this. getTop ()/2, this. getRight (), (this. getBottom ()-this. getTop ()/2, paint );}

3. For example, how can we add a lineColor attribute (the color of the strikethrough) when using xml?> First, create the attrs. xml file under values;> then add the corresponding property and type as needed> use the key code: <? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <! -- Declare-styleable: Declares the style type; attr name = "" declares the attribute name; format = "attribute type" --> <declare-styleable name = "CustomEditText"> <attr name = "lineColor" format = "color"/> </declare-styleable> </resources> note: for the attribute value of format, see * Appendix 1 called in xml: the following code must be declared; otherwise, the attribute name defined earlier cannot be found, and can be declared in the root layout, you can also declare xmlns: medmedittext = "http://schemas.android.com/apk/res/com.anqiansong.androidcustomview" when calling (if there is no declaration, the system will also prompt When referencing the property and then automatically generate as prompted will be xmlns: app = ..... (This mode) Description: xmlns: *** = "http://schemas.android.com/apk/res/packagename" *** is mainly needed in the call, such as android: textColor = "# ffffff "; here, CustomEditText: lineColor = "# ff0000" CustomEditText in "android" method 1, mylinecolor in method 2, and packagename is the current project package name. If the two are not in this format, in addition, since the lineColor value set in xml cannot be referenced, the color value of this attribute must be obtained in the constructor of the custom view. For example, the Code: public class CustomEditText extends EditText {private int lineColor; // global variable of strikethrough color public CustomEditText (Context context) {super (context ); // TODO Auto-generated constructor stub} public CustomEditText (Context context, AttributeSet attrs) {super (context, attrs); Paint paint = new Paint (); TypedArray array = context. obtainStyledAttributes (attrs, R. styleable. customEditText); lineColor = array. getColor (R. styleable. customEditText_lineColor, Color. BLACK); // obtain the color value of the strikethrough set in xml. CustomEditText_lineColor here is an array composed of the name and attribute name and "_" in styleable in attrs. recycle ();} public CustomEditText (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle ); // TODO Auto-generated constructor stub} @ Overrideprotected void onDraw (Canvas canvas) {// TODO Auto-generated method stubsuper. onDraw (canvas); Paint paint = new Paint (); // creates a paint brush object. setColor (lineColor); // you can specify the paint color. setAntiAlias (true); // you can specify whether the image is anti-sawtooth paint. setStrokeWidth (2); // set the width of the paint brush // set the starting point of the X axis and canvas of the Y axis. drawLine (this. getLeft (), (this. getBottom ()-this. getTop ()/2, this. getRight (), (this. getBottom ()-this. getTop ()/2, paint);} Method 1: <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: medmedittext = "http://schemas.android.com/apk/res/com.anqiansong.androidcustomview" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <com. anqiansong. views. customEditText android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Custom EditText" CustomEditText: lineColor = "# ff0000"/> </RelativeLayout> Method 2: <com. anqiansong. views. customEditText xmlns: mylinecolor = "http://schemas.android.com/apk/res/com.anqiansong.androidcustomview" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Custom EditText" mylinecolor: lineColor = "# ff0000"/> In the same principle, we can add a lineHeight attribute to the width of the strikethrough line. The Code is as follows: add an attribute: <attr name = "lineHeight" format = "dimension"/> reference attribute: mylinecolor: lineHeight = "5dp" Get lineHeight in custom view and set the width: lineHeight = array. getDimension (R. styleable. customEditText_lineHeight, 2); In the ondraw () method, paint. setStrokeWidth (lineHeight); // you can specify the width of the paint brush.

Appendix 1: Appendix 1: detailed explanation of the format of Custom Attributes in Android (reference: http://www.cnblogs.com/zhangs1986/p/3243040.html, the author is drunk, please respect original) 1. reference: refer to a resource ID. (1) attribute definition: <declare-styleable name = "name"> <attr name = "background" format = "reference"/> </declare-styleable> (2) attribute usage: <ImageView android: layout_width = "42dip" android: layout_height = "42dip" android: background = "@ drawable/image ID"/> 2. color: color value. (1) attribute definition: <declare-styleable name = "name"> <attr name = "textColor" format = "color"/> </declare-styleable> (2) attribute usage: <TextView android: layout_width = "42dip" android: layout_height = "42dip" android: textColor = "#00FF00"/> 3. boolean: boolean value. (1) attribute definition: <declare-styleable name = "name"> <attr name = "focusable" format = "boolean"/> </declare-styleable> (2) attribute usage: <Button android: layout_width = "42dip" android: layout_height = "42dip" android: focusable = "true"/> 4. dimension: dimension value. (1) attribute definition: <declare-styleable name = "name"> <attr name = "layout_width" format = "dimension"/> </declare-styleable> (2) attribute usage: <Button android: layout_width = "42dip" android: layout_height = "42dip"/> 5. float: floating point value. (1) attribute definition: <declare-styleable name = "AlphaAnimation"> <attr name = "fromAlpha" format = "float"/> <attr name = "toAlpha" format = "float"/> </ declare-styleable> (2) attribute usage: <alpha android: fromAlpha = "1.0" android: toAlpha = "0.7"/> 6. integer: integer value. (1) attribute definition: <declare-styleable name = "AnimatedRotateDrawable"> <attr name = "visible"/> <attr name = "frameDuration" format = "integer"/> <attr name = "framesCount" format = "integer"/> <attr name = "pivotX"/> <attr name = "Ty"/> <attr name = "drawable"/> </declare-styleable> (2) property use: <animated-rotate xmlns: android = "http://schemas.android.com/apk/res/android" android: drawable = "@ drawable/picture ID "android: shorttx =" 50% "android: shortty =" 50% "android: framesCount =" 12 "android: frameDuration =" 100 "/> 7. string: string. (1) attribute definition: <declare-styleable name = "MapView"> <attr name = "apiKey" format = "string"/> </declare-styleable> (2) attribute usage: <com. google. android. maps. mapView android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: apiKey = "0jokq80od1jl9c6haja99ugxcri2cgjko_bc_g"/> 8. fraction: percentage. (1) attribute definition: <declare-styleable name = "RotateDrawable"> <attr name = "visible"/> <attr name = "fromDegrees" format = "float"/> <attr name = "toDegrees" format = "float"/> <attr name = "pivotX" format = "fraction"/> <attr name = "Ty" format = "fraction"/> <attr name =" drawable "/> </declare-styleable> (2) property use: <rotate xmlns: android = "http://schemas.android.com/apk/res/android" android: interpo Lator = "@ anim/animation ID" android: fromDegrees = "0" android: toDegrees = "360" android: shorttx = "200%" android: shortty = "300%" android: duration= "5000" android: repeatMode = "restart" android: repeatCount = "infinite"/> 9. enum: enumeration value. (1) attribute definition: <declare-styleable name = "name"> <attr name = "orientation"> <enum name = "horizontal" value = "0"/> <enum name = "vertical" value = "1"/> </attr> </declare-styleable> (2) attribute usage: <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> </LinearLayout> 10. flag: bit or operation. (1) attribute definition: <declare-styleable name = "name"> <attr name = "windowSoftInputMode"> <flag name = "stateUnspecified" value = "0"/> <flag name = "stateUnchanged" value = "1"/> <flag name = "stateHidden" value = "2"/> <flag name = "stateAlwaysHidden" value = "3"/> <flag name = "stateVisible "value =" 4 "/> <flag name =" stateAlwaysVisible "value =" 5 "/> <flag name =" adjustUnspecified "value =" 0x00 "/> <f Lag name = "adjustResize" value = "0x10"/> <flag name = "adjustPan" value = "0x20"/> <flag name = "adjustNothing" value = "0x30"/> </attr> </declare-styleable> (2) attribute usage: <activity android: name = ". styleAndThemeActivity "android: label =" @ string/app_name "android: windowSoftInputMode =" stateUnspecified | stateUnchanged | stateHidden "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> Note: multiple types of values can be specified during attribute definition. (1) attribute definition: <declare-styleable name = "name"> <attr name = "background" format = "reference | color"/> </declare-styleable> (2) attribute usage: <ImageView android: layout_width = "42dip" android: layout_height = "42dip" android: background = "@ drawable/image ID | #00FF00"/>


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.