1 在attrs.xml中加入
<declare-styleable name="MultiProgressBar"> <attr name="progress" format="integer"/> </declare-styleable>

2 修改构造函数,加入如下代码
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MultiProgressBar); this.currentValue = a.getInt(R.styleable.MultiProgressBar_progress, 0); a.recycle();
3.在代码中加入数据绑定注解(测试数据绑定会报错,但是正常使用属性是可以的)
@BindingAdapter({"progress"}) public void setProgress(View v,int progress){ this.currentValue = progress; update(); }

4.以上都没有问题后就可以在xml中使用了。看到progress出现了吗?

本文介绍了如何在Android中创建一个自定义的MultiProgressBar,并通过attrs.xml设置进度属性,构造函数获取属性值,以及使用数据绑定注解动态更新进度。在XML布局文件中成功应用此自定义View并展示进度。
2035

被折叠的 条评论
为什么被折叠?



