如何更改进度条填充颜色而不是默认的绿色

时间:2013-11-28 04:29:45

标签: android user-interface android-ui

我想根据进度条的%年龄更改颜色。 这是我的代码:

XML

<org.package.VerticalProgressBar
    android:id="@+id/Temp"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="30dp"
    android:layout_height="120dp"
    android:layout_gravity="left"
    android:layout_marginBottom="5dip"
    android:layout_marginLeft="51dip"
    android:maxWidth="100dp"
    android:minWidth="0dp"/>

这就是我在我的应用中使用它的方式:

private static VerticalProgressBar progressBar;
progressBar=(VerticalProgressBar) findViewById(R.id.Temp);
progressBar.setProgress(12);

我应该在此处添加什么来设置进度条的颜色?

1 个答案:

答案 0 :(得分:0)

请查看此tutorial - custom progressBar in android

// 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="#000001"
                    android:centerColor="#0b131e"
                    android:centerY="1.0"
                    android:endColor="#0d1522"
                    android:angle="270"
            />
        </shape>
    </item>

 <!-- Define the progress properties like start color, end color etc -->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <gradient
                    android:startColor="#007A00"
                    android:centerColor="#007A00"
                    android:centerY="1.0"
                    android:endColor="#06101d"
                    android:angle="270"
                />
            </shape>
        </clip>
    </item>

</layer-list>

并从中分配您的java代码 -

// Get the Drawable custom_progressbar                     
Drawable customDrawable= res.getDrawable(R.drawable.custom_progressbar);

// set the drawable as progress drawavle

progressBar.setProgressDrawable(customDrawable);
相关问题