Android动态更改进度条颜色

时间:2013-12-21 18:15:39

标签: android colors

如何从SQLite Int值设置ProgressBar的颜色? 这段代码对我不起作用。

myProgressB.setProgressColor(cb.color);

1 个答案:

答案 0 :(得分:1)

您必须在更新时从其他线程更改它。我通常通过AsyncTask线程执行此操作,在下载文件或执行其他工作时,我将覆盖onProgressUpdate

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
 protected Long doInBackground(URL... urls) {
     int count = urls.length;
     long totalSize = 0;
     for (int i = 0; i < count; i++) {
         totalSize += Downloader.downloadFile(urls[i]);
         publishProgress((int) ((i / (float) count) * 100));
         // Escape early if cancel() is called
         if (isCancelled()) break;
     }
     return totalSize;
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);

//你的工作

     myProgressB.setProgressColor(cb.color);



 }

 protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
 }

}