2012-04-30 26 views
6

我想要有2個指標的進度條。帶2個指標的進度條

一個指示器以綠色顯示任務A的進度,第二個指示器顯示任務B以紅色顯示的進度,全部在一個進度欄中顯示。其餘顯示任務A和B的其餘部分。

是否有一個(簡單)解決方案來實現這一目標?我閱讀了文檔,但沒有找到幫助。

回答

9

這可以通過將兩個指標編碼爲同一進度條的主進度和二級進度來完成。

爲進度條創建子類。

public class TextProgressBar extends ProgressBar { 
    private Paint textPaint; 

    public TextProgressBar(Context context) { 
     super(context); 
     textPaint = new Paint(); 
     textPaint.setColor(Color.BLACK); 
    } 

    public TextProgressBar(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     textPaint = new Paint(); 
     textPaint.setColor(Color.BLACK); 
     setMax(30); 
     setProgress(12); 
     setSecondaryProgress(20); 

    } 

} 

必須使用此子類引用進度條的XML條目。

<com.darsh.doubleProgressBar.TextProgressBar 
    android:id="@+id/progressBar1" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="15sp" 
    android:layout_marginLeft="1sp" 
    android:layout_marginRight="1sp" 
    android:layout_marginTop="10sp" 
    android:progressDrawable="@drawable/progress" /> 

現在創建在資源目錄

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 

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

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


      <gradient 
       android:angle="270" 
       android:centerColor="#32cd32" 
       android:centerY="0.75" 
       android:endColor="#32cd32" 
       android:startColor="#32cd32" /> 
     </shape> 
    </clip> 
</item> 
<item android:id="@android:id/progress"> 
    <clip> 
     <shape> 
      <corners android:radius="5dip" /> 

      <gradient 
       android:angle="270" 
       android:endColor="#33B5E5" 
       android:startColor="#33B5E5" /> 
     </shape> 
    </clip> 
</item> 
</layer-list> 

用於初級和二級指標的顏色可以在該抽拉改變提拉。在你的代碼

使用它們像這樣:

TextProgressBar textProgress; 
textProgress = (TextProgressBar)findViewById(R.id.progressBar1); 
textProgress.setMax(100); 
textProgress.setProgress(10); // 
textProgress.setSecondaryProgress(50); //green 
+0

2簡單的問題:'機器人:ID = 「@機器人:ID/secondaryProgress」',應該說是'機器人:ID = 「@ + ID/secondaryProgress」'?爲什麼要創建一個子類?你創建一個私人領域,但不要使用它。 –

+0

無視第一個問題,我明白了:您需要設置Android設置的ID,以便讓ProgressBar知道使用哪個形狀。 –

2

進行自定義鋪平並將兩個進度條放入其中。然後你需要通過獲取句柄來操縱進度條。如果它的通知,你必須將其設置爲遠程查看。

+2

我不明白你的意思 – user1324936