码迷,mamicode.com
首页 > 移动开发 > 详细

android 代码设置progressBar 颜色

时间:2015-10-09 18:16:01      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:


void test() {
LinearLayout linearLayout = new LinearLayout(this);
ProgressBar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
setColors(progressBar,
0xff0000FF, //bgColor blue
0xffFF0000 //progressColor red
);

progressBar.setProgress(50);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_VERTICAL;
params.leftMargin = 10;
params.rightMargin = 10;
params.height = 10;
linearLayout.addView(progressBar, params);
setContentView(linearLayout);
}

public void setColors(ProgressBar progressBar, int backgroundColor, int progressColor) {
//Background
ClipDrawable bgClipDrawable = new ClipDrawable(new ColorDrawable(backgroundColor), Gravity.LEFT, ClipDrawable.HORIZONTAL);
bgClipDrawable.setLevel(10000);
//Progress
ClipDrawable progressClip = new ClipDrawable(new ColorDrawable(progressColor), Gravity.LEFT, ClipDrawable.HORIZONTAL);
//Setup LayerDrawable and assign to progressBar
Drawable[] progressDrawables = {bgClipDrawable, progressClip/*second*/, progressClip};
LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);
progressLayerDrawable.setId(0, android.R.id.background);
progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
progressLayerDrawable.setId(2, android.R.id.progress);

progressBar.setProgressDrawable(progressLayerDrawable);
}

android 代码设置progressBar 颜色

标签:

原文地址:http://www.cnblogs.com/songsyl1207/p/4864652.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!