13

Tôi đã thêm một thanh tiến trình để hoạt động của tôi bằng cách sử dụng đoạn mã sau:Làm thế nào để thay đổi màu sắc của Bar Progress trong Android - (Tôi đã thử một cách, và nó không làm việc)

<LinearLayout 
    android:id="@+id/linlaHeaderProgress" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:visibility="gone" > 

    <ProgressBar 
     android:id="@+id/pbHeaderProgress" 
     android:indeterminateOnly="true" 
     android:keepScreenOn="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 
    </ProgressBar> 
</LinearLayout> 

Sau đó, Tôi gọi nó bằng cách:

progressbar = (LinearLayout) findViewById(R.id.linlaHeaderProgress); 
progressbar.setVisibility(View.VISIBLE); 

Thanh tiến trình được hiển thị và tôi muốn thay đổi màu của nó. Theo mặc định, thanh tiến trình được hiển thị bằng màu xám. Dưới đây là những gì tôi đã cố gắng để thay đổi màu sắc:

Tôi tạo ra một file xml trong thư mục drawables và đặt tên nó như activityindicator.xml Nội dung xml này là:

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

    <item android:id="@android:id/secondaryProgress"> 


     <color android:color="#f58233" /> 
    </item> 
    <item android:id="@android:id/progress"> 

     <color android:color="#f58233" /> 
    </item> 

</layer-list> 

Và tôi đã thay đổi các tập tin bố trí như sau:

<LinearLayout 
    android:id="@+id/linlaHeaderProgress" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:progressDrawable="@drawable/activityindicator" 
    android:orientation="vertical" 
    android:visibility="gone" > 

    <ProgressBar 
     android:id="@+id/pbHeaderProgress" 
     android:indeterminateOnly="true" 
     android:keepScreenOn="true" 
     android:progressDrawable="@drawable/activityindicator" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 
    </ProgressBar> 
</LinearLayout> 

Đây là những gì tôi đã thử, nhưng màu sắc không thay đổi. Bất cứ ai có thể cho tôi biết tôi đang làm gì sai?

Tôi đang sử dụng phiên bản Lollipop.

Trả lời

52

Nếu bạn chỉ muốn thay đổi màu sắc, thêm một bộ lọc màu để thanh tiến trình của bạn:

pbHeaderProgress.getIndeterminateDrawable().setColorFilter(Color.RED, Mode.MULTIPLY); 

Tham số chế độ, đề cập đến giá trị PorterDuff.Mode - sẵn here.

+1

này cho phép linh hoạt trong việc lựa chọn colors.xml cũng progressBar.getProgressDrawable() setColorFilter (ContextCompat.getColor (getActivity(), R.color.product_status_color), PorterDuff.Mode.MULTIPLY) –

+1

Tôi muốn bạn se Color.WHITE và dòng này không hoạt động. Tôi đã phải thay đổi chế độ porter duff sang SRC_ATOP –

+1

Phương pháp này thay đổi màu sắc của tất cả các thanh tiến trình trong ứng dụng –

8

Tôi vừa tìm được cách. Tôi thậm chí không cần một tập tin xml riêng biệt để thay đổi màu sắc như thanh tiến trình là loại "không xác định: true"

tôi đã sử dụng sau đây để thay đổi màu sắc của thanh tiến trình của tôi:

pbHeaderProgress.getIndeterminateDrawable().setColorFilter(Color.parseColor("#C0D000"), android.graphics.PorterDuff.Mode.SRC_ATOP); 

bạn có thể nhận được nhiều mã hex màu từ đây:. http://www.nthelp.com/colorcodes.htm hoặc http://www.color-hex.com/

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