Use of Custom Attributes in andriod and Custom Attributes in andriod

Source: Internet
Author: User

Use of Custom Attributes in andriod and Custom Attributes in andriod

Custom property file values/attrs. xml:

<?xml version="1.0" encoding="utf-8"?><resources>     <declare-styleable name="RangeSeekBar">              <attr name="orientation" format="string"/>         <attr name="limitThumbRange" format="boolean"/>                           <attr name="scaleMin" format="float"/>        <attr name="scaleMax" format="float"/>        <attr name="scaleStep" format="float"/>                <attr name="thumb" format="reference"/>        <attr name="thumbs" format="integer"/>        <attr name="thumbWidth" format="dimension"/>        <attr name="thumbHeight" format="dimension"/>                <attr name="track" format="reference"/>                <attr name="range" format="reference"/>    </declare-styleable></resources>

Attr in the custom property file is a number of custom properties that can be used in the control properties in the layout file.

The attribute formats include the following:

1. reference: refer to a resource ID.

2. color: color value.

3. boolean: boolean value.

4. dimension: dimension value.

5. float: floating point value.

6. integer: integer value.

7. string: string.

8. fraction: percentage.

9. enum: enumeration value.

10. flag: bitwise OR operation.

Reference custom attributes in the layout file:

1. Define a reference prefix for an attribute: xmlns: RangeSeekBar = "http://schemas.android.com/apk/res/diy.example.location ",
Diy. example. location is the package name. In the following control, you can use the prefix RangeSeekBar. In the control, you can use the prefix to reference attributes.
2. Reference properties in the control:
RangeSeekBar:thumb="@drawable/thumb" 
RangeSeekBar:thumbs="2"
Thumb is a reference type. It references a resource thumb of drawable, that is, a resource file named thumb exists in the drawable directory.
Thumbs is an integer type and is initialized to 2. The following is an example of the layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:RangeSeekBar="http://schemas.android.com/apk/res/diy.example.location"     xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"       tools:context=".ViewLocPathActivity" >       <diy.example.location.RangeSeekBar        android:id="@+id/seekPath"        android:layout_width="match_parent"        android:layout_height="30dp"        android:layout_alignParentBottom="true"        android:layout_marginBottom="50dp"        android:layout_marginLeft="10dp"        android:layout_marginRight="10dp"        RangeSeekBar:thumb="@drawable/thumb"         RangeSeekBar:track="@drawable/trackgradient"         RangeSeekBar:range="@drawable/rangegradient"         RangeSeekBar:thumbs="2"  />    </RelativeLayout>

Reference to custom attributes in java code:

GetFloat is used for floating point numbers, and getDrawable is used for reference. Each attribute name will generate an integer under R. styleable for reference in java code:

scaleRangeMin = a.getFloat(R.styleable.RangeSeekBar_scaleMin, 0);
Thumb = a. getDrawable (R. styleable. RangeSeekBar_thumb); below is the sample code for using custom attributes in a custom control:
    public RangeSeekBar(Context context, AttributeSet attrs) {        super(context, attrs);        init(context);        // Obtain our styled custom attributes from xml        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.RangeSeekBar);                        String s = a.getString(R.styleable.RangeSeekBar_orientation);        if(s != null)            orientation = s.toLowerCase().contains("vertical") ? VERTICAL : HORIZONTAL;                limitThumbRange = a.getBoolean(R.styleable.RangeSeekBar_limitThumbRange, true);                scaleRangeMin = a.getFloat(R.styleable.RangeSeekBar_scaleMin, 0);        scaleRangeMax = a.getFloat(R.styleable.RangeSeekBar_scaleMax, 100);        scaleStep = Math.abs(a.getFloat(R.styleable.RangeSeekBar_scaleStep, DEFAULT_STEP));                thumb = a.getDrawable(R.styleable.RangeSeekBar_thumb);                range = a.getDrawable(R.styleable.RangeSeekBar_range);                track = a.getDrawable(R.styleable.RangeSeekBar_track);                // Register desired amount of thumbs        int noThumbs = a.getInt(R.styleable.RangeSeekBar_thumbs, DEFAULT_THUMBS);        thumbWidth = a.getDimension(R.styleable.RangeSeekBar_thumbWidth, 50);        thumbHeight = a.getDimension(R.styleable.RangeSeekBar_thumbHeight, 100);        for(int i = 0; i < noThumbs; i++) {            Thumb th = new Thumb();            thumbs.add(th);        }        a.recycle();    }

-------------------------------------------------------------

Other brilliant articles

Android KSOAP2 call. net webservicejQuery tutorial (8)-DOM tree operation using reverse Insertion Method android learning notes (34) using AlertDialog to create a simple dialog box android learning notes (33) Gallery view (Gallery) android navidgation drawer in the navigation drawer how to change the List of selected items...

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.