編輯:Android 開發實例
本文記錄了在 Android 中自定義 ProgressBar 顏色的完整過程,供大家參考。
先看效果圖:

由於產品經理的各種自定義需求,我查閱了不少資料,下面就把整個學習與實現過程梳理出來。
要改進度條顏色,最徹底的办法是去讀源碼。好在下載了源碼,在 sources\base\core\res\res\ 下可謂應有盡有。因為是修改樣式,首先要找到 styles.xml。進去之後可以看到系統默認的轉圈樣式:
<style name="Widget.ProgressBar"> <item name="android:indeterminateOnly">true</item> <item name="android:indeterminateDrawable">@android:drawable/progress_medium_white</item> <item name="android:indeterminateBehavior">repeat</item> <item name="android:indeterminateDuration">3500</item> <item name="android:minWidth">48dip</item> <item name="android:maxWidth">48dip</item> <item name="android:minHeight">48dip</item> <item name="android:maxHeight">48dip</item> </style>
但今天我們要改的是水平進度條的顏色,所以要繼續往下找到:
<style name="Widget.ProgressBar.Horizontal"> <item name="android:indeterminateOnly">false</item> <item name="android:progressDrawable">@android:drawable/progress_horizontal</item> <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item> <item name="android:minHeight">20dip</item> <item name="android:maxHeight">20dip</item> </style>
可以看到系統的關聯設計層層遞進、低耦合、擴展性很好。真正負責「畫」進度條的是 android:progressDrawable,因此我們只要找到 drawable 下的 progress_horizontal 文件並修改它,就能改變進度條顏色:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="https://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#ff9d9e9d" android:centerColor="#ff5a5d5a" android:centerY="0.75" android:endColor="#ff747674" android:angle="270" /> </shape> </item> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#80ffd300" android:centerColor="#80ffb600" android:centerY="0.75" android:endColor="#a0ffcb00" android:angle="270" /> </shape> </clip> </item> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#ffffd300" android:centerColor="#ffffb600" android:centerY="0.75" android:endColor="#ffffcb00" android:angle="270" /> </shape> </clip> </item> </layer-list>
看到沒有,這就是系統進度條的繪製布局。關鍵色值就在這幾行:
android:startColor="#80ffd300" android:centerColor="#80ffb600" android:endColor="#ff747674"
我們只要修改 <item android:id="@android:id/progress"> 下的色值,就能改變進度條的顏色。說了這麼多,具體該怎麼做呢?其實很簡單:
1. 在我們的項目下新建一個 style.xml,創建一個 style 標簽並繼承系統默認樣式,然後引用一個自定義的 progressDrawable 文件:
<style name="ProgressBar_Mini" parent="@android:style/Widget.ProgressBar.Horizontal"> <item name="android:maxHeight">50dip</item> <item name="android:minHeight">8dip</item> <item name="android:indeterminateOnly">false</item> <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item> <item name="android:progressDrawable">@drawable/progressbar_mini</item> </style>
2. 下面是我自定義的 progressbar_mini 文件,改動了 android:endColor 和 android:startColor 的色值:
<layer-list xmlns:android="https://schemas.android.com/apk/res/android" > <item android:id="@android:id/background"> <shape > <corners android:radius="5dip" /> <gradient android:angle="270" android:centerY="0.75" android:endColor="#F5F5F5" android:startColor="#BEBEBE" /> </shape> </item> <item android:id="@android:id/secondaryProgress"> <clip > <shape > <corners android:radius="0dip" /> <gradient android:angle="270" android:centerY="0.75" android:endColor="#165CBC" android:startColor="#85B0E9" /> </shape> </clip> </item> <item android:id="@android:id/progress"> <clip > <shape > <corners android:radius="5dip" /> <gradient android:angle="270" android:centerY="0.75" android:endColor="#165CBC" android:startColor="#85B0E9" /> </shape> </clip> </item> </layer-list>
最後,在布局中引用這個自定義樣式即可:
<ProgressBar android:id="@+id/progress" style="@style/ProgressBar_Mini" android:layout_width="fill_parent" android:layout_height="wrap_content" android:progress="50" />
效果如下:

總結一下:自定義進度條顏色的關鍵,就是找到並修改 progressDrawable 這個 layer-list 中的漸變色值,再透過自定義 style 引用它。理解了這個原理,之後想換成任何配色都游刃有餘了。
希望本文所述對大家的 Android 程序設計有所幫助。
Android Permission 權限級別與組件保護
Android 權限不僅用於安裝宣告,也可用於保護組件間通訊。不同 protectionLevel 會影響授權方式與風險提示。常見權限級別normal 屬於低風險權限,安
Android Matrix 詳解:縮放、旋轉、傾斜與 set/post/pre 的差別
Matrix,中文叫矩陣,高等數學裡有介紹。在圖片處理上,它主要負責平面的縮放、平移、旋轉這類線性變換。Android 的 android.graphics.Matrix
Android 登入範例:安全提示、錯誤處理與按鈕停用
登入功能通常包含使用者名稱、密碼與登入按鈕。Android 中可用 EditText 建立輸入欄位,並將密碼欄位的 inputType 設為 textPassword。
Android ApiDemo 系列一:建立範例專案
Android SDK 內附的 ApiDemo 是學習 Android API 的重要資源。它把大量範例依功能分類,例如 App、Content、Graphics、Med