2011-06-04 32 views
7

我需要設置二次進度條的顏色編程Android的進度:如何設置輔助色編程

我只看到該方法

ProgressBar.setProgressDrawable(drawable) 

爲設定的原色,但是不存在用於設置二次色的方法。

我該怎麼做?

+1

米歇爾,是U找到解決辦法嗎? IM插得太深這個問題:( – Peter 2011-12-27 15:23:49

+0

@Peter看到我的回答如下。 – Michele 2011-12-28 14:58:28

回答

2

的繪製指定使用setProgressDrawable有背景,在其初級和次級可繪。以下是隨與Android的example of ProgressBar drawable

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:id="@android:id/background"> 
     <shape> 
      <corners android:radius="5dip" /> 
      <gradient ... /> 
     </shape> 
    </item> 

    <item android:id="@android:id/secondaryProgress"> 
     <clip> 
      <shape> 
       <corners android:radius="5dip" /> 
       <gradient .../> 
      </shape> 
     </clip> 
    </item> 

    <item android:id="@android:id/progress"> 
     <clip> 
      <shape> 
       <corners android:radius="5dip" /> 
       <gradient ... /> 
      </shape> 
     </clip> 
    </item> 

</layer-list> 
+0

您好,感謝的答覆。 但是我決不能使用XML和Java這樣做。 – Michele 2011-06-06 07:39:24

+0

我也想在java做的,但創建兩個XML文件(或更多,因爲你的需要),並選擇你想要的是那麼容易,快速和乾淨,我堅持這種方法。 – 2015-01-28 00:32:21

4

ProgressBar.getProgressDrawable()返回LayerDrawable,其中:

LayerDrawable progressDrawable = (LayerDrawable) getProgressDrawable(); 
Drawable backgroundColor = progressDrawable.getDrawable(0); 
Drawable secondaryColor = progressDrawable.getDrawable(1); 
Drawable primaryColor = progressDrawable.getDrawable(2); 

我嘗試修改顏色白衣(例如):

progressDrawable.setDrawableByLayerId(progressDrawable.getId(2), new ClipDrawable(...)); 

但有時屏幕凍結,有時會崩潰。最後我放棄了對時間不夠的搜索。 對不起:(

米歇爾

+0

正是我需要的,這真棒! – reidisaki 2016-07-25 23:53:40

2

可能不相關的米歇爾,但也許這將幫助別人..

ProgressBar progress = (ProgressBar) fullView.findViewById(R.id.main_progressbar); 
LayerDrawable progressDrawable = (LayerDrawable)progress.getProgressDrawable(); 
Drawable drawable = activity.getResources().getDrawable(R.drawable.WHATEVER_YOU_WANT); 
ClipDrawable cd = new ClipDrawable(drawable, Gravity.LEFT,ClipDrawable.HORIZONTAL); 
progressDrawable.setDrawableByLayerId(android.R.id.progress, cd); 
0

入住這

您可以使用一個可繪製這樣的XML set progressdrawableable

<item android:id="@android:id/background" 
    android:drawable="@drawable/progress_bar_nor">  
</item>  
<item android:id="@android:id/secondaryProgress" 
    android:drawable="@drawable/progress_bar_nor">  
</item> 
<item 
    android:id="@android:id/progress" 
    android:drawable="@drawable/progress_bar_sel"/> 

請參考以下鏈接

https://stackoverflow.com/a/27144594/1554031

相關問題