Android自定义ProgressBar样式

最近用到webView加载网页的功能,但是在空白页面等的太久了于是就添加一个进度条缓解一下等待的焦虑
添加之后发现系统的ProgressBar的样式有点不尽如人意,所以就自定义了一个样式,先来看看效果图:


代码:
1,首先在styles.xml中增加自定义的样式MyProgressBar

 <style name="MyProgressBar" parent="@android:style/Widget.ProgressBar.Horizontal">
        <item name="android:maxHeight">5dip</item>
        <item name="android:minHeight">2dip</item>
        <item name="android:indeterminateOnly">false</item>
        <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
        <item name="android:progressDrawable">@drawable/progressbar_bg</item>
    </style>


2,在drawable中新建progressbar_bg.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>
            <corners android:radius="5dip" />
            <gradient
                android:angle="0"
                android:endColor="#00000000"
                android:startColor="#00000000" />
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <scale
                android:scaleWidth="100%"
                android:drawable="@drawable/my_title_bg"/>
        </clip>
    </item>
</layer-list>

3,在这里的背景图我用了一个渐变资源也是在drawable中my_title_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--left to right-->
    <gradient
        android:angle="0"

        android:startColor="@color/color_FE8E51"
        android:endColor="@color/color_FF4358"/>
</shape>


到这里基本资源就搞定了,接下来在布局文件中使用

<ProgressBar
    android:id="@+id/lh_pb"
    style="@style/MyProgressBar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_2" />


运行之后就可以看到效果啦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值