Đ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
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ó. –
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. –