【发布时间】: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