In-depth parsing of custom Android attribute format

Source: Internet
Author: User

The following is a detailed analysis of the custom attribute format in Android. For more information, see

1. reference: refer to a resource ID.
(1) attribute definition:

Copy codeThe Code is as follows:
<Declare-styleable name = "name">
<Attr name = "background" format = "reference"/>
</Declare-styleable>


(2) attribute usage:

Copy codeThe Code is as follows:
<ImageView
Android: layout_width = "42dip"
Android: layout_height = "42dip"
Android: background = "@ drawable/image ID"
/>


2. color: color value.
(1) attribute definition:

Copy codeThe Code is as follows:
<Declare-styleable name = "name">
<Attr name = "textColor" format = "color"/>
</Declare-styleable>


(2) attribute usage:

Copy codeThe Code is as follows:
<TextView
Android: layout_width = "42dip"
Android: layout_height = "42dip"
Android: textColor = "#00FF00"
/>


3. boolean: boolean value.
(1) attribute definition:

Copy codeThe Code is as follows:
<Declare-styleable name = "name">
<Attr name = "focusable" format = "boolean"/>
</Declare-styleable>


(2) attribute usage:

Copy codeThe Code is as follows:
<Button
Android: layout_width = "42dip"
Android: layout_height = "42dip"
Android: focusable = "true"
/>


4. dimension: dimension value.
(1) attribute definition:

Copy codeThe Code is as follows:
<Declare-styleable name = "name">
<Attr name = "layout_width" format = "dimension"/>
</Declare-styleable>


(2) attribute usage:

Copy codeThe Code is as follows:
<Button
Android: layout_width = "42dip"
Android: layout_height = "42dip"
/>


5. float: floating point value.
(1) attribute definition:

Copy codeThe Code is as follows:
<Declare-styleable name = "AlphaAnimation">
<Attr name = "fromAlpha" format = "float"/>
<Attr name = "toAlpha" format = "float"/>
</Declare-styleable>


(2) attribute usage:

Copy codeThe Code is as follows:
<Alpha
Android: fromalphi = "1.0"
Android: toAlpha = "0.7"
/>


6. integer: integer value.
(1) attribute definition:

Copy codeThe Code is as follows:
<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:

Copy codeThe Code is as follows:
<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:

Copy codeThe Code is as follows:
<Declare-styleable name = "MapView">
<Attr name = "apiKey" format = "string"/>
</Declare-styleable>


(2) attribute usage:

Copy codeThe Code is as follows:
<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:

Copy codeThe Code is as follows:
<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:

Copy codeThe Code is as follows:
<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:

Copy codeThe Code is as follows:
<Declare-styleable name = "name">
<Attr name = "orientation">
<Enum name = "horizontal" value = "0"/>
<Enum name = "vertical" value = "1"/>
</Attr>
</Declare-styleable>


(2) attribute usage:

Copy codeThe Code is as follows:
<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:

Copy codeThe Code is as follows:
<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:

Copy codeThe Code is as follows:
<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:

Copy codeThe Code is as follows:
<Declare-styleable name = "name">
<Attr name = "background" format = "reference | color"/>
</Declare-styleable>


(2) attribute usage:

Copy codeThe Code is as follows:


<ImageView
Android: layout_width = "42dip"
Android: layout_height = "42dip"
Android: background = "@ drawable/image ID | #00FF00"
/>

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.