【问题标题】:How to dynamically change the color of progress bar background android如何动态改变进度条背景android的颜色
【发布时间】:2011-06-02 22:03:26
【问题描述】:

我想动态更改 android 中进度条的背景颜色。我遵循了本教程页面末尾附近的“奖励”部分:

http://colintmiller.com/2010/10/how-to-add-text-over-a-progress-bar-on-android/

它会改变颜色,但只会改变一次。如果多次调用,进度条就会消失。这是代码。

这是进度条的定义:

<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/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>

rex/values/colors.xml 的条目:

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

最后,在代码中:

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

同样,问题是它第一次工作,但随后使栏消失。

【问题讨论】:

  • 包含可绘制定义的文件的确切名称是什么?你的问题是它的'res/drawable/green_xml',但你将drawable设置为R.drawable.green_progress。如果您将可绘制对象设置为 R.drawable.green_progress,我希望您的文件名是“res/drawable/green_progress.xml”
  • 谢谢,没错。我刚刚更正了。
  • 你在哪里调用 m_bar.setProgressDrawable(...)?你是在 onCreate(..) 里面做的吗?
  • @james - 不幸的是,不,这只是帖子上的一个错字。
  • @james - 我最初在从 onResume 调用的方法中设置它,但随后从按钮单击调用相同的方法。

标签: android colors progress-bar


【解决方案1】:

答案见Android ProgressBar.setProgressDrawable only works once?,重复如下:

我发现当调用 setprogressdrawable 时,drawable 不知道它的大小。当它最初设置时,它确实知道它的大小。这意味着搜索栏有一个新的可绘制对象集,但可绘制对象的大小为 0,您将看不到任何内容。

解决方法是先获取当前drawable的bounds,然后设置新的drawable,最后再设置bounds:

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

【讨论】:

  • 啊,有道理。我想我误解了你一开始想做什么。我在尝试进行自定义地图叠加时实际上遇到了同样的问题/修复。不同的情况,但 Drawable 的相同潜在问题不知道它的大小。 +1
  • James,之前有没有发现这个问题?我查看了 ProgressBar 的源代码,但它做了一些看起来很难理解的模糊操作,即 requestLayout() 和 postInvalididate()。我想知道您如何跟踪解决方案以供将来参考。提前致谢!
  • 因此,我结合使用调试器和层次结构查看器工具来跟踪问题。在显示了我的 ImageView(它使用了可绘制对象)之后,我在 hierarchyviewer 中查找了该视图以确保它在那里,它就是。我注意到它的维度是 0。然后我搜索了堆栈溢出。
  • 啊,对。所以你已经为调试器加载了 Android 源代码?也许我应该试一试。
  • 不,我没有加载android源代码进行调试。我只是单步执行了我自己的代码,我在其中动态添加视图。调试更多地与确保事情正确发生有关,它们是,更多的是层次结构查看器引导我走下 0 维路径。调试器始终是我首先转向的地方。但是,如果您想了解 'invalidate()' 和其他方法的作用,那么调试源代码绝对是可行的方法。快乐探险:)
【解决方案2】:

好吧,对于任何正在寻找如何以编程方式进行操作的人:

    Drawable bckgrndDr = new ColorDrawable(Color.RED);
    Drawable secProgressDr = new ColorDrawable(Color.GRAY);
    Drawable progressDr = new ScaleDrawable(new ColorDrawable(Color.BLUE), Gravity.LEFT, 1, -1);
    LayerDrawable resultDr = new LayerDrawable(new Drawable[] { bckgrndDr, secProgressDr, progressDr });

    resultDr.setId(0, android.R.id.background);
    resultDr.setId(1, android.R.id.secondaryProgress);
    resultDr.setId(2, android.R.id.progress);

    progressBar.setProgressDrawable(resultDr);

将 ids 设置为 drawables 至关重要,并负责保留进度条的边界和​​实际状态

【讨论】:

  • 作品完美。只需按照此代码并根据需要更改颜色即可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-10
  • 2014-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-20
相关资源
最近更新 更多