自定义View的属性设置

1、values文件夹创建atts.xml文件,添加

1 <resources>
2     <declare-styleable name="控件名">
3         <attr name = "属性名" format = "类型名"/>
4     </declare-styleable>
5 </resources>

2、创建控件类,重写有AttributeSet参数的构造方法。

3、创建TypedArray对象

//创建对象
TypedArray ta = context.obtainStyledAttributes(attrs,R.styeable.控件名);

//获取属性
ta.get类型(R.styleable.控件名_属性名);
ta.recycle();

4、在布局文件中调用

<控件名
custom:属性名="value"
/>

 

posted @ 2017-05-08 17:39  大当弟弟  阅读(422)  评论(0)    收藏  举报