以编程方式设置进度条进度颜色

时间:2014-09-18 11:14:42

标签: android android-progressbar

我在下载文件时显示进度条。我想设置来自服务器的进度的颜色(HTML颜色代码),并且可能会不时地改变颜色代码。 我如何满足要求。

image

例如,对于上面的图像,我想改变黄色并设置服务器的颜色。

2 个答案:

答案 0 :(得分:1)

所以...我做了一个改变进度颜色的示例项目,你也可以改变背景进度条的颜色。

首先创建一个自定义进度条xml文件。

<强> custom_progressbar.xml

<!-- 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="#2A98E1"
                android:centerColor="#2A98E1"
                android:centerY="1.0"
                android:endColor="#06101d"
                android:angle="270"
                />
        </shape>
    </clip>
</item>

活动布局:

<强> activity_my.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MyActivity">


<ProgressBar
    android:id="@+id/myProgress"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_weight="0.7"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/progressTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:text="None"/>

</LinearLayout>

测试活动:

重要的是这个:

Drawable draw = getResources()。getDrawable(R.drawable.custom_progressbar); progressBar.setProgressDrawable(绘制);

<强> MyActivity.java

private ProgressBar progressBar;
private Handler handler = new Handler();
private int progressStatus = 0;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);



    init();
    initProgresBar();

}


public void init(){

    textView = (TextView) findViewById(R.id.progressTextView);
    progressBar = (ProgressBar) findViewById(R.id.myProgress);
    Drawable draw=getResources().getDrawable(R.drawable.custom_progressbar);
     // set the drawable as progress drawable
    progressBar.setProgressDrawable(draw);

}

public void initProgresBar(){
    new Thread(new Runnable() {
        public void run() {
            while (progressStatus < 100) {
                progressStatus += 1;
                // Update the progress bar and display the

                //current value in the text view
                handler.post(new Runnable() {
                    public void run() {
                        progressBar.setProgress(progressStatus);
                        textView.setText(progressStatus+"/"+progressBar.getMax());
                    }
                });
                try {
                    // Sleep for 200 milliseconds.

                    //Just to display the progress slowly
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();

}

输出将是:

enter image description here

希望这就是你在寻找的地方。干杯

有关详细信息,您会发现这个有用:stack progress bar post

答案 1 :(得分:0)

progressBar.getProgressDrawable().setColorFilter(Color.parseColor("#YourColorHex"), Mode.SRC_IN);

Tutorial for Customizing ProgressBar

修改

http://miroprocessordev.blogspot.in/2012/09/android-how-to-change-progress-bar.html