Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自訂水平進度條:打造黑色虛線風格的 ProgressBar

編輯:關於 Android 開發

自訂水平進度條:打造黑色虛線風格的 ProgressBar

系統預設的進度條樣式較為制式,若想讓介面更具個人風格,可以透過自訂 Drawable 來改造外觀。本文示範如何製作一個黑色虛線風格的水平進度條。

一、在 layout 中使用

<ProgressBar
android:id="@+id/progress_bar"
... />

二、自訂樣式 myprogress.xml

核心在於使用 layer-list 搭配 shape 的 stroke 屬性,透過 dashWidth 與 dashGap 控制虛線的長度與間距:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<!--item background 與 progress 順序不可顛倒-->
<item android:id="@android:id/background">
<shape android:shape="line">
<stroke
android:width="4dp"
android:color="#666666"
android:dashWidth="4dp"
android:dashGap="1dp" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape android:shape="line">
<stroke
android:width="4dp"
android:color="#000000"
android:dashWidth="4dp"
android:dashGap="1dp" />
</shape>
</clip>
</item>
</layer-list>

其中 background 定義進度條底色(灰色虛線),progress 則以 clip 包裹已完成部分(黑色虛線),兩者順序不可顛倒。調整 dashWidth 與 dashGap 即可改變虛線的疏密,讓進度條呈現俐落的科技感。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved