Detailed description of Custom Attributes in Android

Source: Internet
Author: User

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"

/>

 

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"

/>

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.