码迷,mamicode.com
首页 > 其他好文 > 详细

andriod中自定义属性的使用

时间:2015-01-04 22:58:53      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

自定义属性文件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都是一些自定义属性,可以在layout文件中的控件属性中使用。

属性的format有以下几种:

1. reference:参考某一资源ID。

2. color:颜色值。

3. boolean:布尔值。

4. dimension:尺寸值。

5. float:浮点值。

6. integer:整型值。

7. string:字符串。

8. fraction:百分数。

9. enum:枚举值。

10. flag:位或运算。

 

 

layout文件中引用自定义属性:

1. 定义一个属性的引用前缀:xmlns:RangeSeekBar="http://schemas.android.com/apk/res/diy.example.location" ,
diy.example.location为包名。下面在控件中可以用前缀RangeSeekBar下面在控件中可以用前缀来引用属性。

2. 在控件中引用属性:
RangeSeekBar:thumb="@drawable/thumb" 
RangeSeekBar:thumbs="2"
thumb是reference类型,引用drawable的一个资源thumb,即在drawable目录下有一个名为thumb的资源文件存在。
thumbs是整数类型,初始化为2。

下面是layout文件范例:
<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>

 

java代码中对自定义属性的引用:

根据属性的类型不同引用方式不同,浮点数用getFloat,reference用getDrawable。每个属性名称都会在R.styleable下生成一个整数值用来在java代码中引用:

scaleRangeMin = a.getFloat(R.styleable.RangeSeekBar_scaleMin, 0);
thumb = a.getDrawable(R.styleable.RangeSeekBar_thumb);
下面是自定义控件中使用自定义属性的范例代码:
    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();
    }

 

andriod中自定义属性的使用

标签:

原文地址:http://www.cnblogs.com/jindouyun/p/4202331.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!