2012-04-30 34 views
6

Tôi muốn có thanh tiến trình với 2 chỉ báo.Thanh tiến trình với 2 Chỉ số

Một chỉ báo cho biết tiến trình nhiệm vụ A có màu xanh lá cây, chỉ báo thứ hai cho biết tiến trình của nhiệm vụ B màu đỏ, tất cả trong một thanh tiến trình. Phần còn lại cho thấy phần còn lại của các nhiệm vụ A và B.

Có một giải pháp (đơn giản) nào để đạt được điều này không? Tôi đã đọc tài liệu nhưng không tìm thấy trợ giúp.

Trả lời

9

Điều này có thể được thực hiện bằng cách mã hóa hai chỉ báo dưới dạng Tiến trình chính và tiến trình phụ của cùng một thanh tiến trình.

tạo một lớp con cho thanh tiến trình.

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); 

    } 

} 

Mục nhập XML cho thanh tiến trình phải được tham chiếu bằng cách sử dụng lớp con này.

<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" /> 

tại tạo drawable trong thư mục nguồn

<?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> 

Những màu sắc cho các chỉ số chính và phụ có thể được thay đổi trong drawable này.

Make sử dụng chúng trong mã của bạn như thế này:

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

2 câu hỏi nhanh: 'android: id = "@ android: id/secondaryProgress"', mà nên được ' android: id = "@ + id/secondaryProgress" '? Và tại sao tạo một phân lớp? Bạn tạo một trường riêng, nhưng không sử dụng nó. –

+0

Bỏ qua câu hỏi đầu tiên, tôi đã nhận được: Bạn cần đặt id từ tập hợp Android, để cho ProgressBar biết hình dạng nào cần sử dụng. –

2

Tạo bố cục tùy chỉnh và đặt hai thanh tiến trình vào trong đó. Sau đó, bạn cần phải thao tác các progressbars bằng cách xử lý của họ. Nếu có thông báo, bạn phải đặt thông báo đó làm Chế độ xem từ xa.

+2

tôi không hiểu những gì bạn có nghĩa là – user1324936

Các vấn đề liên quan