(六十八)进度条自定义样式(例如进度颜色、背景描边、填充、圆角)的方法

一、说明

<1>在某项延续性工作的进展过程中为了不让用户觉得程序死掉了,需要有个活动的进度条,表示此过程正在进行中。
<2>在某些操作的进度中的可视指示器,为用户呈现操作的进度,还它有一个次要的进度条,用来显示中间进度,如在流媒体播放的缓冲区的进度。一个进度条也可不确定其进度。在不确定模式下,进度条显示循环动画。这种模式常用于应用程序使用任务的长度是未知的。

二、ProgressBar的两个进度

一个是android:progress;另一个是android:secondaryProgress:主要是而为了缓存所涉及的,比如在看网络视频都会有一个缓存的进度条以及一个播放的进度,在这里缓存的进度就可以是android:secondaryProgress,而播放进度就是android:progress。

三、设置进度条高度为7dp;背景为1px描边#eaeaea,填充色#f5f5f5,圆角2dp;进度颜色为#ff9d45,圆角4px的方法。

3.1 在drawable文件加下写一个文件progress_bar_gradient.xml,文件中的代码如下所示:

<?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 android:shape="rectangle" >
            <corners
                android:bottomLeftRadius="2dp"
                android:bottomRightRadius="2dp"
                android:topLeftRadius="2dp"
                android:topRightRadius="2dp" />

            <stroke
                android:width="1px"
                android:color="#eaeaea" />

            <solid android:color="#f5f5f5" />
        </shape>
    </item>
    <!-- 进度条上的背景 -->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners
                    android:bottomLeftRadius="2dp"
                    android:bottomRightRadius="2dp"
                    android:topLeftRadius="2dp"
                    android:topRightRadius="2dp" />

                <solid android:color="#ff9d45" />
            </shape>
        </clip>
    </item>

</layer-list>

3.2 在xml文件中使用ProgressBar,代码如下:

 <ProgressBar
            android:id="@+id/pb_my_package"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="7dp"
            android:layout_marginTop="7dp"
            android:progress="50"
            android:progressDrawable="@drawable/progress_bar_gradient" />

3.3 通过上面的代码设置后,进度条大致的效果如下所示:

原文地址:https://www.cnblogs.com/fuyanan/p/4593044.html