需求:要求带有文字的彩色进度条
一/自定义进度条.class
public classMyProgressBarextendsLinearLayout {
@Bind(R.id.exp_textView)
TextViewexpTextView;
@Bind(R.id.progressbar_layout)
LinearLayoutprogressbarLayout;
private intmProgress;
private intmMax;
publicMyProgressBar(Context context) {
super(context);
}
publicMyProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.control_progressbar,this,true);
ButterKnife.bind(this);
}
public synchronized voidsetMax(intmax) {
if(max <0) {
max =0;
}
if(max !=mMax) {
mMax= max;
postInvalidate();
if(mProgress> max) {
mProgress= max;
}
refreshProgress();
}
}
public synchronized voidsetProgress(intprogress) {
if(progress ==mProgress) {
// No change from current.
return;
}
mProgress= progress;
refreshProgress();
}
private voidrefreshProgress() {
floatscale =mMax>0? (float)mProgress/ (float)mMax:0;
//TODO: 当要使用 weight 时 width 必须设置成 0
expTextView.setLayoutParams(newLinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, scale));
}
}
二/布局文件control_progressbar.xml
android:id="@+id/progressbar_layout"
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="@drawable/shape_progressbar_bg"
android:weightSum="1">
android:id="@+id/exp_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="@drawable/shape_exp_progressbar"
android:includeFontPadding="false"
android:paddingLeft="7dp"
android:paddingRight="27dp"
android:text="300/2000"
android:textColor="@android:color/white"
android:textSize="8sp"/>
三/彩色颜色背景 shape_exp_progressbar.xml
android:startColor="@color/exp_progressBar_left"
android:endColor="@color/exp_progressBar_right"/>