How to use Declare-styleable

Source: Internet
Author: User

How to use Declare-styleable

  1. In the create XML to objectname/res/values/named Attrs.xml
  2. Edit file contents as (example)
    <?xml version= "1.0" encoding= "Utf-8"?>
    <resources>
    <declare-styleable name= "Test1" >
    <attr name= "TEST" format = "float" ></attr>
    </declare-styleable>
    </resources>
  3. in the definition file in the Layout folder, refer to at Use
        <org.xiaoyunduo.mytext
             & nbsp xmlns:app= "Http://schemas.android.com/apk/res/org.xiaoyunduo"
            android:id= "@+id/add"
             android:layout_width= "wrap_content"
            android:layout_height= " Wrap_content "
            app:test=" 100 "
            android:text= "@string/log_text_box_1_add_text"/>
    // I remember I didn't need to define xmlns. Now you have to define the form that you can use android:test directly, and you now have to define xmlns.
  4. Get the values in the layout file in code (preferably called in the constructor method)
    TypedArray a = Context.obtainstyledattributes (Attrs, r.styleable.test1);//typedarray is an array container
    float textSize = a.getfloat (r.styleable.test1_test, 30);//Prevent no definition in XML file, add default value 30
  5. Initializes the obtained value to the control
    This does not write in the construction method of the value has been obtained, the back of the love how to use it.

Declare-styleable Definition Description:

1. Reference: Refer to a resource ID.

(1) Attribute definition:

<declare-styleable name = "Names" >

<attr name = "Background" format = "Reference"/>

</declare-styleable>

(2) attribute use:

<imageview

Android:layout_width = "42dip"
Android:layout_height = "42dip"
Android:background = "@drawable/Picture ID"

/>

2. Color: Colour value.

(1) Attribute definition:

<declare-styleable name = "Names" >

<attr name = "TextColor" format = "Color"/>

</declare-styleable>

(2) attribute use:

<textview

Android:layout_width = "42dip"
Android:layout_height = "42dip"
Android:textcolor = "#00FF00"

/>

3. Boolean: Boolean value.

(1) Attribute definition:

<declare-styleable name = "Names" >

<attr name = "focusable" format = "Boolean"/>

</declare-styleable>

(2) attribute use:

<button

Android:layout_width = "42dip"
Android:layout_height = "42dip"

Android:focusable = "true"

/>

4. Dimension: Dimension value.

(1) Attribute definition:

<declare-styleable name = "Names" >

<attr name = "layout_width" format = "Dimension"/>

</declare-styleable>

(2) attribute use:

<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 use:

<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 = "Pivoty"/>
<attr name = "Drawable"/>

</declare-styleable>

(2) attribute use:

<animated-rotate

xmlns:android = "Http://schemas.android.com/apk/res/android"
android:drawable = "@drawable/Picture ID"
Android:pivotx = "50%"
Android:pivoty = "50%"
Android:framescount = "12"
android:frameduration = "100"

/>

7. String: Strings.

(1) Attribute definition:

<declare-styleable name = "Mapview" >
<attr name = "ApiKey" format = "string"/>
</declare-styleable>

(2) attribute use:

<com.google.android.maps.mapview
Android:layout_width = "Fill_parent"
Android:layout_height = "Fill_parent"
Android:apikey = "0jokq80od1jl9c6haja99ugxcris2cgjko_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 = "Pivoty" format = "fraction"/>
<attr name = "Drawable"/>
</declare-styleable>

(2) attribute use:

<rotate

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

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

Android:pivotx = "200%"

Android:pivoty = "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 use:

<linearlayout

xmlns:android = "Http://schemas.android.com/apk/res/android"
Android:orientation = "vertical"
Android:layout_width = "Fill_parent"
Android:layout_height = "Fill_parent"
>
</LinearLayout>

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"/>
<flag name = "Adjustresize" value = "0x10"/>
<flag name = "Adjustpan" value = "0x20"/>
<flag name = "adjustnothing" value = "0x30"/>
</attr>

</declare-styleable>

(2) attribute use:

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

Attention:

Multiple types of values can be specified when the property is defined.

(1) Attribute definition:

<declare-styleable name = "Names" >

<attr name = "Background" format = "Reference|color"/>

</declare-styleable>

(2) attribute use:

<imageview

Android:layout_width = "42dip"
Android:layout_height = "42dip"
Android:background = "@drawable/Picture id| #00FF00"

/>

Define namespaces

You can also customize the namespace when you customize the extension, but I don't feel right when I'm imitating the demo. The properties in the namespace cannot be alt+/by a hint, but the demo can.

The namespace definition syntax is as follows: (because I did not complete the Drag_layout.xml code in this post)
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
xmlns:dot= "Http://schemas.android.com/apk/res/com.example.android.apis"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:orientation= "Vertical"
>

How to use Declare-styleable

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.