自定义控件分三步:
1.自定义属性的声明与获取:
(1)找到value文件夹,新建一个名为attrs的xml文件:
由图可以看出:需要打造的水平进度条的progressbar需要有哪些自定义属性呢?基本需要七个属性:
a.左边已完成的进度reachbar需要高度reachbarheight
b.左边已完成的进度reachbar需要颜色reachbarcolor
c.中间的文字需要大小textsize
d.中间的文字需要颜色textcolor
e.中间的文字需要与Bar之间的间隔textoffset
f.右边未完成的进度unreachbar需要颜色unreachbarcolor
g.右边未完成的进度unreachbar需要高度unreachbarheight
所以我们需要对这七个属性进行声明
<attr name="progress_unreach_color" format="color"></attr> <attr name="progress_unreach_height" format="dimension"></attr> <attr name="progress_reach_color" format="color"></attr> <attr name="progress_reach_height" format="dimension"></attr> <attr name="progress_text_size" format="dimension"></attr> <attr name="progress_text_color" format="color"></attr> <attr name="progress_text_offset" format="dimension"></attr>
声明后,我们需要新建一个继承自ProgressBar 的view命名为HorizontalProgressbar,然后在attrs文件中的declare-styleable标签中进行使用:
<declare-styleable name="HorizontalProgressBar"> <attr name="progress_unreach_color" ></attr> <attr