2015-02-09 78 views
0

我想爲圓角的進度條設置不同的顏色。下面是我的代碼..如何設置進度欄顏色?

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

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

       <solid android:color="@color/purple" /> 
      </shape> 
     </clip> 

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

       <solid android:color="@color/purple" /> 
      </shape> 
     </clip> 

     <color android:color="@color/purple" /> 
    </item> 

</layer-list> 

<ProgressBar 
     android:id="@+id/progress_bar" 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical" 
     android:progressDrawable="@drawable/progress_bar" 
     android:visibility="visible" /> 

但是,這是行不通的。仍然進度條顯示爲灰色。 請幫我,我哪裏錯了?

+0

檢查這個答案http://stackoverflow.com/questions/6421178/how-to-change-default-color-of-progress-bar。 – Bae 2015-02-09 09:58:16

回答

0

在res目錄下創建一個xml文件夾,並將progress.xml文件放入其中。

progress.xml

<?xml version="1.0" encoding="utf-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0" 
    android:toDegrees="360"> 
    <shape android:shape="ring" android:innerRadiusRatio="3" 
     android:thicknessRatio="8" android:useLevel="false"> 

     <size android:width="76dip" android:height="76dip" /> 
     <gradient android:type="sweep" android:useLevel="false" 
      android:startColor="#447a29" 
      android:endColor="#447a29" 
      android:angle="0" 
      /> 
    </shape> 
</rotate> 

集startColor和ENDCOLOR按您選擇。

現在在progressbar的背景中設置progress.xml。

像這樣

<ProgressBar 
    android:id="@+id/ProgressBar01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background ="@xml/progress"> 
0

創建你的水庫命名customprogressbar.xml的XML文件>繪製文件夾:

customprogressbarcolour.xml

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

    <!-- Define the background properties like color etc --> 
    <item android:id="@android:id/background"> 
    <shape> 
     <gradient 
       android:startColor="#000001" 
       android:centerColor="#0b131e" 
       android:centerY="1.0" 
       android:endColor="#0d1522" 
       android:angle="270" 
     /> 
    </shape> 
    </item> 

    <!-- Define the progress properties like start color, end color etc --> 
    <item android:id="@android:id/progress"> 
    <clip> 
     <shape> 
      <gradient 
       android:startColor="#007A00" 
       android:centerColor="#007A00" 
       android:centerY="1.0" 
       android:endColor="#06101d" 
       android:angle="270" 
      /> 
     </shape> 
    </clip> 
    </item> 
</layer-list> 

做好以下的XML:

<ProgressBar 
    android:id="@+id/progress1" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:progressDrawable="@drawable/custom_progressbar"   
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

在運行時

Drawable draw=res.getDrawable(R.drawable.customprogressbarcolour); 
    progressBar.setProgressDrawable(draw);