android color progressbar 帶有文字的彩色進度條

需求:要求帶有文字的彩色進度條


一/自定義進度條.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"/>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容