Android Custom Attributes

Source: Internet
Author: User

 

Learning Control attributes in Android custom XML

 

Preface:

In the process of large-scale application development, in order to satisfy various UI Styles, you must require your own applications to have their own unified style interface styles. Otherwise, unexpected results may occur when the system style is used.

For example, the button control may be black-white characters in android2.3, and android2.4 may be white-black characters. Therefore, during the UI design, we cannot design whether the UI background is black or white-white, as a result, we naturally want to design our own button UI. That is, you can develop your own UI controls. When reading the android API, you will always see attributeset attrs in the method parameters. This attribute parameter does not know what it is. This document is formed after some related information is obtained from the Internet.

 

 

1. Define control attributes in an XML file

We are used to Android: attrs = "", so can we define our own properties, such as: Test: attrs =? The answer is yes. For example:

Define an attrs. xml file under the Res/values file. The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>

<Resources>

<Declare-styleable name = "myview">

<ATTR name = "textcolor" format = "color"/>

<ATTR name = "textsize" format = "dimension"/>

</Declare-styleable>

</Resources>

2. Implementation of constructor in Java code

The code in myview. Java is modified as follows:

Public myview (context, attributeset attrs)

{

Super (context, attrs );

Mpaint = new paint ();

    

Typedarray A = context. obtainstyledattributes (attrs,

R. styleable. myview );

Int textcolor = A. getcolor (R. styleable. myview_textcolor,

0 xffffffff );

Float textsize = A. getdimension (R. styleable. myview_textsize, 36 );

    

Mpaint. settextsize (textsize );

Mpaint. setcolor (textcolor );

A. Recycle ();

}

 

All the code for myview. Java is as follows:

Package com. Android. tutor;

Import Android. content. context;

Import Android. content. res. typedarray;

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 Wei's blog ";

         

Public myview (context ){

Super (context );

Mpaint = new paint ();

}

Public myview (context, attributeset attrs)

{

Super (context, attrs );

Mpaint = new paint ();

             

Typedarray A = context. obtainstyledattributes (attrs,

R. styleable. myview );

Int textcolor = A. getcolor (R. styleable. myview_textcolor,

0 xffffffff );

Float textsize = A. getdimension (R. styleable. myview_textsize, 36 );

             

Mpaint. settextsize (textsize );

Mpaint. setcolor (textcolor );

A. Recycle ();

}

@ Override

Protected void ondraw (canvas ){

// Todo auto-generated method stub

Super. ondraw (canvas );

// 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 );

}

}

We get the defined attributes. sytleable. myview_textcolor. The default value (float textsize =. getdimension (R. styleable. myview_textsize, 36);) to prevent definition in the XML file. to use the default value! Retrieving myview is the name defined in <declare-styleable name = "myview"> </declare-styleable>. You can use the name_attribute to obtain the attributes in the myview. You can connect to the myview.
; Usually, the. Recycle () method is called at the end. In order to keep the future use of this attribute consistent!

Iii. Reference Custom Attributes

Add the custom myview to the layout main. xml file, and use the Custom Attributes. The Custom Attributes must be added:

Xmlns: test = "http://schemas.android.com/apk/res/com.android.tutor" Blue is the prefix of custom properties, red is our package name.

The code for Main. XML is as follows:

<? XML

Version = "1.0" encoding = "UTF-8"?>

<Linearlayout

Xmlns: Android = "http://schemas.android.com/apk/res/android"

Xmlns: test = "http://schemas.android.com/apk/res/com.android.tutor"

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"

Test: textsize = "20px"

Test: textcolor = "# fff"

/>

</Linearlayout>

 

Iv. Explanation of custom attribute formats in Android

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: fromalphi = "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) attribute usage:

<Animated-Rotate

Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: drawable = "@ drawable/image ID"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: framescount = "12"
Android: frameduration = "100" type = "codeph" text = "codeph"/>

 

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 = "effectx" format = "fraction"/>
<ATTR name = "ty" format = "fraction"/>
<ATTR name = "drawable"/>
</Declare-styleable>

(2) attribute usage:

<Rotate

Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: interpolator = "@ anim/animation ID"

Android: fromdegrees = "0"
Android: todegrees = "360"

Android: Required Tx = "200%"

Android: Ty = "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: bitwise 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"/>
<Flag 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:

You can specify multiple types of values when defining attributes.

(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"

/>

 

V. References 

1. http://weizhulin.blog.51cto.com/1556324/311453

2.

Http://blog.csdn.net/mayingcai1987/article/details/6216655

 

 

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.