andriod中自定义属性的使用 - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 技术分享 >

andriod中自定义属性的使用

发表时间:2020-10-19

发布人:葵宇科技

浏览次数:29


自定义属性文件values/attrs.xml:

[img]http://common.cnblogs.com/images/copycode.gif
[img]http://common.cnblogs.com/images/copycode.gif
<?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>

[img]http://common.cnblogs.com/images/copycode.gif
自定义属性文件中的attr都是一些自定义属性,可以在layout文件中的控件属性中应用。
属性的format有以下几种:
根据属性的类型不合引用方法不合,浮点数用getFloat,reference用getDrawable。每个属性名称都邑在R.styleable下生成一个整数值用来在java代码中引用:
1. reference:参考某一资本ID。
2. color:色彩值。
3. boolean:布尔值。
4. dimension:尺寸值。
5. float:浮点值。
6. integer:整型值。
7. string:字符串。
8. fraction:百分数。
9. enum:列举值。
10. flag:位或运算。
RangeSeekBar:thumb="@drawable/thumb" 

layout文件中引用自定义属性:
1. 定义一个属性的引用前缀:xmlns:RangeSeekBar="http://schemas.android.com/apk/res/diy.example.location" ,
diy.example.location为包名。下面在控件中可以用前缀RangeSeekBar下面在控件中可以用前缀来引用属性。
thumb是reference类型,引用drawable的一个资本thumb,即在drawable目次下有一个名为thumb的资本文件存在。
2. 在控件中引用属性:
RangeSeekBar:thumbs="2"
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>

[img]http://common.cnblogs.com/images/copycode.gif
java代码中对自定义属性的引用:
scaleRangeMin = a.getFloat(R.styleable.RangeSeekBar_scaleMin, 0);
thumb = a.getDrawable(R.styleable.RangeSeekBar_thumb);
下面是自定义控件中应用自定义属性典范例代码:

[img]http://common.cnblogs.com/images/copycode.gif
    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();
    }

[img]http://common.cnblogs.com/images/copycode.gif
-------------------------------------------------------------
其他出色文┞仿文┞仿

Android KSOAP2调用.net webservice

jQuery教程(8)-DOM树操作之应用反向插入办法

android进修标记(34)应用AlertDialog创建简单对话框

android进修标记(33)画廊视图(Gallery)的功能和用法

android navidgation drawer 在导航抽屉中若何改变List选中项的...


更多关于android开辟文┞仿

相关案例查看更多