自定义View的属性设置

2016-08-23  本文已影响0人  大当弟弟

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

<resources>
    <declare-styleable name="控件名">
        <attr name = "属性名" format = "类型名"/>
    </declare-styleable>
</resources>

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

3、创建TypedArray对象


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

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

4、在布局文件中调用

<控件名
custom:属性名="value"
/>
上一篇 下一篇

猜你喜欢

热点阅读