ProgressBar自定义样式

本文介绍如何使用Android自定义通知栏中的进度条样式,包括设置进度条的最大值、当前值及是否只显示不确定进度等属性,并通过代码实现动态更新进度。
     <ProgressBar
            android:id="@+id/custom_progressbar"
            style="@style/StyleProgressBarMini"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
        android:background="@drawable/shape_progressbar_bg"
            android:max="100"
            android:progress="0" />

    <!-- progressbar -->
    <style name="StyleProgressBarMini" parent="@android:style/Widget.ProgressBar.Horizontal">
        <item name="android:maxHeight">20dip</item>
        <item name="android:minHeight">5dip</item>
        <item name="android:indeterminateOnly">false</item>
        <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
        <item name="android:progressDrawable">@drawable/shape_progressbar_mini</item>
    </style>

//@drawable/shape_progressbar_mini
<?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="270"
                android:centerY="0.75"
                android:endColor="#FFFFFF"
                android:startColor="#FFFFFF" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="0dip" />

                <gradient
                    android:angle="270"
                    android:centerY="0.75"
                    android:endColor="#df0024"
                    android:startColor="#df0024" />
            </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="@color/top_bar"
                    android:startColor="@color/top_bar" />
            </shape>
        </clip>
    </item>

</layer-list>
![这里写图片描述](http://img.blog.youkuaiyun.com/20151007110526607)


//在通知栏中显示ProgressBar
/**
     * 初始化要用到的系统服务
     */
    private void initService() {
        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    /**
     * 显示状态栏进度条
     */
    public void showCustomProgressNotify() {
        initService();
        mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.custom_notification_progress);
        mRemoteViews.setImageViewResource(R.id.custom_progress_icon, R.drawable.ic_launcher);
        Intent intent = new Intent(DOWNLOADFAILED_ACTION);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        mRemoteViews.setOnClickPendingIntent(R.layout.custom_notification_progress, pendingIntent);
        PendingIntent pi = getDefalutIntent(0);
        mNotification = new Notification.Builder(context).setAutoCancel(true).setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pi).build();
        mNotification.contentView = mRemoteViews;
        mRemoteViews.setProgressBar(R.id.custom_progressbar, 100, 0, false);
        mNotificationManager.notify(NOTIFYID, mNotification);

    }

    /**
     * 下载更新状态栏进度条
     */
    Handler downloadHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == DOWNLOADING ||msg.what == DOWNLOADSUCCESS) {
                mRemoteViews.setTextViewText(R.id.tv_custom_progress_title, context.getString(R.string.down_start));
                mRemoteViews.setProgressBar(R.id.custom_progressbar, 100, progress, false);
                mRemoteViews.setTextViewText(R.id.tv_custom_progress_percent, String.valueOf(progress));
                if (progress == 100) {
                    mRemoteViews.setTextViewText(R.id.tv_custom_progress_title, context.getString(R.string.down_success));
                }
                mNotification.contentView = mRemoteViews;
                mNotificationManager.notify(0, mNotification);
            }else if (msg.what == DOWNLOADFAILED) {
                mRemoteViews.setTextViewText(R.id.tv_custom_progress_title, context.getString(R.string.down_failed));
                mRemoteViews.setProgressBar(R.layout.custom_notification_progress, 100, progress, false);
                mNotification.contentView = mRemoteViews;
                mNotificationManager.notify(0, mNotification);
            }
            super.handleMessage(msg);

        }

    };
这个由于是从项目中提取的,所以有些是缺失的,我只是记录一下到时候能记起来,基本的方法都在上面了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tracker_wangqin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值