ProgressBar简单使用

最近做项目的时候,做检查更新功能,需要做一个notification的下载进度,就需要使用progressBar控件,本来打算使用自定义的继承自ProgressBar,结果发现不能用,谷歌限制了只能使用特定的系统的progressBar,所以不得自定义样式了。
关于progressBar需要知道的是:

1、ProgressBar有两个进度,一个是Android:progress,另一个是android:secondaryProgress。后者主要是为缓存需要所涉及的,比如在看网络视频时候都会有一个缓存的进度条以及还要一个播放的进度,在这里缓存的进度就可以是android:secondaryProgress,而播放进度就是android:progress,有了secondProgress,可以很方便定制ProgressBar。

2、ProgressBar分为确定的和不确定的,确定的是我们能明确看到进度,相反不确定的就是不清楚、不确定一个操作需要多长时间来完成,这个时候就需要用的不确定的ProgressBar了。属性android:indeterminate如果设置为true的话,那么ProgressBar就可能是圆形的滚动条或者水平的滚动条(由样式决定),但是我们一般时候,是直接使用Style类型来区分圆形还是水平ProgressBar的。

在xml中使用方式:
可以使用系统的样式

   <ProgressBar
            android:id="@+id/progressBar"
            style="?android:progressBarStyleHorizontal"
            android:progress="10"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="3dp" />

或者

        <ProgressBar
            android:id="@+id/progressBar"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:progress="10"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="3dp" />

系统自带的样式
这里写图片描述
系统自带的样式
这里写图片描述
看看系统是如何定义样式的

    <style name="Widget.ProgressBar.Horizontal">
        <item name="indeterminateOnly">false</item>
        <item name="progressDrawable">@drawable/progress_horizontal</item>
        <item name="indeterminateDrawable">@drawable/progress_indeterminate_horizontal</item>
        <item name="minHeight">20dip</item>
        <item name="maxHeight">20dip</item>
        <item name="mirrorForRtl">true</item>
    </style>

发现主要是下面的代码来实现bar的样式(确定的进度)

        <item name="progressDrawable">@drawable/progress_horizontal</item>

我们自定义这部分代码实现

<?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: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="@color/statusbar_bg"
                    android:centerColor="@color/statusbar_bg"
                    android:centerY="0.75"
                    android:endColor="@color/statusbar_bg"
                    android:angle="270"
                    />
            </shape>
        </clip>
    </item>
    <!--进度条-->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                    android:startColor="@color/statusbar_bg"
                    android:centerColor="@color/statusbar_bg"
                    android:centerY="0.75"
                    android:endColor="@color/statusbar_bg"
                    android:angle="270"
                    />
            </shape>
        </clip>
    </item>


</layer-list>

使用啥的就不说了,下面看看圆形进度条
同样的方式:

    <style name="Widget.ProgressBar.Small">
        <item name="indeterminateDrawable">@drawable/progress_small_white</item>
        <item name="minWidth">16dip</item>
        <item name="maxWidth">16dip</item>
        <item name="minHeight">16dip</item>
        <item name="maxHeight">16dip</item>
    </style>

可以看出下面这行代码就是实现了

      <item name="indeterminateDrawable">@drawable/progress_small_white</item>
    <style name="mProgress_circle">
        <item name="indeterminateDrawable">@drawable/ykt_loading</item>
        <item name="minWidth">16dip</item>
        <item name="maxWidth">16dip</item>
        <item name="minHeight">16dip</item>
        <item name="maxHeight">16dip</item>
    </style>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值