2012-12-06 30 views
2

我想從程序中動態更改SeekBar的進度顏色(而不是SeekBar背景)。使用seekbar.setProgressDrawable(Drawable drawable)僅改變SeekBar的背景。我見過的所有例子都使用了XML中的drawable,這些drawable在運行時是靜態的。如何從程序動態更改SeekBar的進度drawable?

顏色根據某些用戶活動而改變,我需要將其設置爲seekbar的進度可繪製。所以我在我的代碼中編寫自己的drawable。

在XML中,我們可以指定項目ID(如背景,進度等)以獲得可繪製的?如何在程序中實現這一點?

回答

1

您可以創建自定義XML,然後setbackgrounddrawable你的進度編程,

myProgressBar.setProgressDrawable(R.drawable.progress_horizo​​ntal)的;

* progress_horizo​​ntal.xml *

<item android:id="@android:id/background"> 
     <shape> 
      <corners android:radius="5dip" /> 
      <gradient 
        android:startColor="#ff9d9e9d" 
        android:centerColor="#ff5a5d5a" 
        android:centerY="0.75" 
        android:endColor="#ff747674" 
        android:angle="270" 
      /> 
     </shape> 
    </item> 

    <item android:id="@android:id/secondaryProgress"> 
     <clip> 
      <shape> 
       <corners android:radius="5dip" /> 
       <gradient 
         android:startColor="#80ffd300" 
         android:centerColor="#80ffb600" 
         android:centerY="0.75" 
         android:endColor="#a0ffcb00" 
         android:angle="270" 
       /> 
      </shape> 
     </clip> 
    </item> 

    <item android:id="@android:id/progress"> 
     <clip> 
      <shape> 
       <corners android:radius="5dip" /> 
       <gradient 
         android:startColor="#ffffd300" 
         android:centerColor="#ffffb600" 
         android:centerY="0.75" 
         android:endColor="#ffffcb00" 
         android:angle="270" 
       /> 
      </shape> 
     </clip> 
    </item> 
+0

正如我在我的問題已經提到這個解決方案犯規解決我的問題。我需要隨時根據用戶輸入更改搜索欄的進度。 XML drawable只能設置一個靜態drawable。即使我使用代碼中的自定義drawable,它只會改變seekbar的背景而不是進度 –

相關問題