在android中设置剩余进度条颜色

时间:2015-02-04 07:41:11

标签: android android-layout android-activity android-progressbar

我使用以下样式代码

设置进度条的颜色
<item android:id="@android:id/progress">
    <clip>
      <shape>
        <corners android:radius="0dip" />
          <gradient
            android:angle="0"
            android:endColor="#36A19C"
            android:startColor="#36A19C" />
      </shape>
    </clip>
  </item>

我的进度条如下图所示

enter image description here
如何设置进度条中显示的灰色部分的颜色???

3 个答案:

答案 0 :(得分:4)

尝试查看一些教程herehere

在drawable中创建 custom_progressbar.xml 文件

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Define the background properties like color etc -->
    <item android:id="@android:id/background">
        <shape>
            <gradient
                    android:startColor="backgroundColor"
                    android:endColor="backgroundColor"
                    android:angle="0"
            />
        </shape>
    </item>

 <!-- Define the progress properties like start color, end color etc -->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <gradient
                    android:startColor="progressColor"
                    android:endColor="progressColor"
                    android:angle="0"
                />
            </shape>
        </clip>
    </item>

</layer-list>

将其设置为代码

中的progressBar
 // Get the Drawable custom_progressbar                     
 Drawable customDrawable= res.getDrawable(R.drawable.custom_progressbar);
 // set the drawable as progress drawavle
 progressBar.setProgressDrawable(customDrawable);

答案 1 :(得分:2)

您可以尝试这种方式:

<?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>
            <solid android:color="@color/seekbar_background" />

            <stroke
                android:width="@dimen/seekbar_background_border"
                android:color="@android:color/transparent" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="@color/seekbar_progress" />
            </shape>
        </clip>
    </item>

</layer-list>

进度条使用包含多个图层的图层列表。您需要背景进度。希望这可以帮助你:)

答案 2 :(得分:0)

将此行代码添加到进度条XML。

function StartThirdPartyScripts() {

    (function (i, s, o, g, r, a, m) {
        i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
            (i[r].q = i[r].q || []).push(arguments)
        }, i[r].l = 1 * new Date(); a = s.createElement(o),
        m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
    })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');

    ga('create', 'UA-XXXXXXXXX-1', 'auto');
    ga('send', 'pageview');

    !function (f, b, e, v, n, t, s) {
        if (f.fbq) return; n = f.fbq = function () {
            n.callMethod ?
            n.callMethod.apply(n, arguments) : n.queue.push(arguments)
        }; if (!f._fbq) f._fbq = n;
        n.push = n; n.loaded = !0; n.version = '2.0'; n.queue = []; t = b.createElement(e); t.async = !0;
        t.src = v; s = b.getElementsByTagName(e)[0]; s.parentNode.insertBefore(t, s)
    }(window, document, 'script', 'https://connect.facebook.net/en_US/fbevents.js');

    fbq('init', 'XXXXXXXXXXXXXXXXXXXX');
    fbq('track', 'PageView');
}

它对我有用。