2011-03-15 54 views

回答

4

使用進度的方法:

public void setProgressDrawable (Drawable d) 

,並以不同的顏色通過繪製對象,恩。其中有setColor(int color)方法的ColorDrawable

要改變的飛行嘗試PNG繪製顏色:How to change colors of a Drawable in Android?


編輯:

進度繪製的是LayerDrawable,看起來像:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@android:drawable/screen_progress_frame" /> 
    <item> 
     <scale scaleWidth="100%" scaleGravity="0x3" drawable="@android:drawable/screen_progress_inner" /> 
    </item> 
</layer-list> 

它ScaleDrawable要上色。

創建類似於上面的自己的LayerDrawable,並提取欄可繪製並更改它的顏色。

+0

嗨,感謝您的回答,雖然這實際上改變了整個進度條的顏色和背景,而不僅僅是它的進度部分。有關如何更改進度條的任何線索? – barata7 2011-03-15 15:44:48

+0

我編輯了答案,是否有幫助? – pawelzieba 2011-03-16 11:05:17

0

這是您可以在代碼中使用的解決方案(基於pawelzieba的解決方案)。

ProgressBar progressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal); 
progressBar.setProgressDrawable(new LayerDrawable(
      new Drawable[] { 
       new ColorDrawable(Color.RED), 
       new ScaleDrawable(new ColorDrawable(Color.GREEN), Gravity.LEFT, 1, -1), 
      })); 
... 
progressBar.setProgress(20); 
0

那麼,對於任何想要以編程方式尋找的人:不要忘記爲您的Drawable設置ID!

Drawable bckgrndDr = new ColorDrawable(Color.RED); 
    Drawable secProgressDr = new ColorDrawable(Color.GRAY); 
    Drawable progressDr = new ScaleDrawable(new ColorDrawable(Color.BLUE), Gravity.LEFT, 1, -1); 
    LayerDrawable resultDr = new LayerDrawable(new Drawable[] { bckgrndDr, secProgressDr, progressDr }); 
    //setting ids is important 
    resultDr.setId(0, android.R.id.background); 
    resultDr.setId(1, android.R.id.secondaryProgress); 
    resultDr.setId(2, android.R.id.progress); 

設置可繪製的id是至關重要的,並且負責保存進度條的邊界和​​實際狀態。

(它seeems由vmayatskii樣的作品提出瞭解決方案 - 但只有當你調用setProgress(值)aftrawards,該值必須是從目前的進度值不同)

相關問題