如何动态地改变进度条的背景机器人的颜色 [英] How to dynamically change the color of progress bar background android

查看:131
本文介绍了如何动态地改变进度条的背景机器人的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态改变Android的进度条的背景色。我跟着附近的本教程的页面末尾的奖金的一部分:

I'd like to dynamically change the background color of a progress bar in android. I followed the "bonus" part near the end of the page of this tutorial:

<一个href="http://colintmiller.com/2010/10/how-to-add-text-over-a-progress-bar-on-android/">http://colintmiller.com/2010/10/how-to-add-text-over-a-progress-bar-on-android/

有改变颜色,但仅一次。如果多次调用,进度条消失。这里的code。

It changes the color, but only once. If called more than once, the progress bar disappears. Here's the code.

下面的进度条的定义:

<ProgressBar android:id="@+id/progress_bar" android:layout_width="fill_parent"
    android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_marginRight="5dp" />

这是在RES /绘制/ green_progress.xml提拉定义:

This is the drawable definition in res/drawable/green_progress.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:startColor="#ff9d9e9d"
                android:centerColor="#ff5a5d5a"
                android:centerY="0.75"
                android:endColor="#ff747674"
                android:angle="270"
        />
    </shape>
</item>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="5dip" />
            <gradient
                    android:startColor="#80ffd300"
                    android:centerColor="#80ffb600"
                    android:centerY="0.75"
                    android:endColor="#a0ffcb00"
                    android:angle="270"
            />
        </shape>
    </clip>
</item>
<item
    android:id="@android:id/progress">
    <clip>
        <shape>
            <corners
                android:radius="5dip" />
            <gradient
                    android:startColor="@color/greenStart"
                    android:centerColor="@color/greenMid"
                    android:centerY="0.75"
                    android:endColor="@color/greenEnd"
                    android:angle="270"
            />
        </shape>
    </clip>
</item>

</layer-list>

烯项雷克斯/价值/ colors.xml:

Ene entries to rex/values/colors.xml:

<color name="greenStart">#ff33dd44</color>
<color name="greenMid">#ff0A8815</color>
<color name="greenEnd">#ff1da130</color>

最后,在code:

And finally, in the code:

m_bar.setProgressDrawable(getResources().getDrawable(R.drawable.green_progress));  

再次的问题是,它的工作原理是第一次,但随后使栏消失

Again, the problem is it works the first time, but then makes the bar disappear.

推荐答案

请参阅<一href="http://stackoverflow.com/questions/2805866/android-progressbar-setprogressdrawable-only-works-once">Android ?ProgressBar.setProgressDrawable只能一次的答案,重复如下:

See Android ProgressBar.setProgressDrawable only works once? for the answer, repeated below:

我发现的是,绘制不知道它的大小,当setprogressdrawable被调用。当最初设置,它不知道它的大小。这意味着有一个新的绘制设置为搜索栏,但绘制的大小为0,你不会看到任何东西。

What I found out is that the drawable doesn't know it's size when setprogressdrawable is called. When it is initially set up, it does know it's size. This means there is a new drawable set to the seekbar, but the size of the drawable is 0, you won't see anything.

解决的办法是先获取当前绘制的边界,然后设置新的绘制,最后再设定范围:

The solution is to first get the bounds of the current drawable, then set the new drawable and finally set the bounds again:

Rect bounds = mySeekBar.getProgressDrawable().getBounds();
mySeekBar.setProgressDrawable(newSeekBarBackground);
mySeekBar.getProgressDrawable().setBounds(bounds);

这篇关于如何动态地改变进度条的背景机器人的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆