Android中自定义属性(attrs.xml,TypedArray)的使用

Class Overview


Container for an array of values that were retrieved with obtainStyledAttributes(AttributeSet, int[], int, int) or obtainAttributes(AttributeSet, int[]). Be sure to call recycle() when done with them. The indices used to retrieve values from this structure correspond to the positions of the attributes given to obtainStyledAttributes.

解析:可以通过obtainStyledAttributes(AttributeSet, int[], int, int) or obtainAttributes(AttributeSet, int[])这两个方法得到

用完需要使用recycle() 释放。


getDimensionPixelSize(int index, int defValue)

Retrieve a dimensional unit attribute at  index for use as a size in raw pixels.

例1:
  
TypedArray a = context.obtainStyledAttributes(attr, R.styleable.myView);//TypedArray是一个数组容器 
28         float textSize = a.getDimension(R.styleable.myView_textSize30);//防止在XML文件里没有定义,就加上了默认值30 
29         int textColor = a.getColor(R.styleable.myView_textColor0xFFFFFFFF);//同上,这里的属性是:名字_属性名,下划线 


2. [代码]attrs.xml     

1 <?xml version="1.0" encoding="utf-8"?> 
2 <resources
3     <declare-styleable name="myView"
4         <attr name="textColor" format="color"/> 
5         <attr name="textSize" format="dimension"/> 
6     </declare-styleable
7 </resources

例2:

TypedArray a=context.obtainStyledAttributes
                       (attrs,R.styleable. com_deezapps_widget_HorizontalPager );
    //TypedArray是一个数组容器 
pageWidthSpec=a.getDimensionPixelSize
                  (R.styleable.com_deezapps_widget_HorizontalPager _ pageWidth, SPEC_UNDEFINED);
    //防止在XML文件里没有定义,就加上了默认值SPEC_UNDEFINED
a.recycle();


attrs.xml里:

<declare-styleable name=" com.deezapps.widget.HorizontalPager ">
        <attr name=" pageWidth " format="dimension" />
    </declare-styleable>




3. [代码]main.xml     

01 <?xml version="1.0" encoding="utf-8"?> 
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
03     xmlns:test="http://schemas.android.com/apk/res/com.adnroid.test" 
04     android:orientation="vertical" 
05     android:layout_width="fill_parent" 
06     android:layout_height="fill_parent" 
07     
08 <TextView   
09     android:layout_width="fill_parent"  
10     android:layout_height="wrap_content"  
11     android:text="@string/hello" 
12     /> 
13 <com.adnroid.test.MyView 
14     android:layout_width="fill_parent" 
15     android:layout_height="fill_parent" 
16     test:textSize="10px" 
17     test:textColor="#fff" 
18     /> 
19 </LinearLayout
 <com.deezapps.widget.HorizontalPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        deezapps:pageWidth="250dip"
        >

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值