使用declare-styleable给自定义控件添加自定义属性
来源:互联网 发布:方太跟帅康哪个好 知乎 编辑:程序博客网 时间:2024/06/04 18:07
1.首先,先写attrs.xml<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="TestAttr"> <attr name="name" format="reference" /> <attr name="age"> <flag name="child" value="10" /> <flag name="young" value="18" /> <flag name="oldman" value="60" /> </attr> <attr name="textSize" format="dimension" /> </declare-styleable></resources>reference指的是是从string.xml引用过来flag是自己定义的,类似于 android:gravity="top"dimension 指的是是从dimension.xml里引用过来的内容.注意,这里如果是dp那就会做像素转换2.在布局文件里的写法<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:attrstest="http://schemas.android.com/apk/res/com.arlos.attrstest" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >s <com.arlos.attrstest.MyTestView android:id="@+id/tvTest" android:layout_width="fill_parent" android:layout_height="wrap_content" attrstest:name="@string/myname" android:gravity="top" attrstest:age="young" attrstest:textSize="@dimen/aa" android:text="@string/hello" /></LinearLayout> 2.1 先引用这个dtdxmlns:attrstest="http://schemas.android.com/apk/res/com.arlos.attrstest"attrstest是随便写的.后面的包名是你所在的项目的根包.也就是在manifest里的com.arlos.attrstest 2.2 在自定义的控件里写属性3. 最后在控件的构造方法里取得这些值public class MyTestView extends TextView { public MyTestView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.TestAttr); String name = tArray.getString(R.styleable.TestAttr_name); System.out.println("name = " + name); int age = tArray.getInt(R.styleable.TestAttr_age, 200); System.out.println("age = " + age); float demin = tArray.getDimension(R.styleable.TestAttr_textSize,0); System.out.println("demin = " + demin); tArray.recycle(); }}
转载自:http://www.cnblogs.com/carlosk/archive/2012/06/06/2538336.html
0 0
- 使用declare-styleable给自定义控件添加自定义属性
- 给自定义控件添加自定义属性--declare-styleable
- declare-styleable自定义控件属性
- 自定义控件,自定义控件属性declare-styleable
- declare-styleable:自定义控件的属性的使用
- Android自定义控件使用declare-styleable进行属性配置
- Android自定义控件使用declare-styleable进行属性配置
- declare-styleable, 自定义属性
- declare-styleable自定义控件
- declare-styleable:自定义控件的属性
- declare-styleable:自定义控件的属性
- declare-styleable:自定义控件的属性
- declare-styleable:自定义控件的属性
- declare-styleable:自定义控件的属性
- declare-styleable:自定义控件的属性
- declare-styleable:自定义控件的属性
- declare-styleable:自定义控件的属性
- declare-styleable:自定义控件的属性
- web切图的实用快捷键
- Java笔记7:最简单的网络请求Demo
- Python标准库05 存储对象 (pickle包,cPickle包)
- Python标准库06 子进程 (subprocess包)
- vim打造成IDE
- 使用declare-styleable给自定义控件添加自定义属性
- Linux进程间通信——使用共享内存
- codeforces 616E Sum of Remainders(数学公式转化 较难)
- eclipse编程环境搭建之一:java
- 关于45°角度地图坐标的计算原理 - LVin_A
- Python标准库07 信号 (signal包,部分os包)
- hdoj 4826 Labyrinth 【dp】
- BigDecimal 使用方法详解
- struts2中拦截器与过滤器的区别