我想动态更改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中的可绘制(drawable)定义:
<?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));
问题是它第一次运行时有效,但然后使柱形图消失。