我想动态更改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" />
Run Code Online (Sandbox Code Playgroud)
这是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>
Run Code Online (Sandbox Code Playgroud)
rex/values/colors.xml的ene条目:
<color name="greenStart">#ff33dd44</color>
<color name="greenMid">#ff0A8815</color>
<color name="greenEnd">#ff1da130</color>
Run Code Online (Sandbox Code Playgroud)
最后,在代码中:
m_bar.setProgressDrawable(getResources().getDrawable(R.drawable.green_progress));
Run Code Online (Sandbox Code Playgroud)
同样,问题是它第一次起作用,但随后使条形消失.
使用setProgressDrawable了ProgressBar具有自定义颜色不给我们以正确的方式工作.我们在a的行中使用progressbars ListView,但仅当列表中有多个元素时才会显示进度.如果是一个元素,则进度条为空.
CursorAdapter.java:
public class CursorAdapter extends SimpleCursorAdapter {
public CursorAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
}
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
updateProgressbar(view, cursor);
}
/**
* This method updates the progressbar using the "numberpages" and
* "currentpage" values.
*/
private void updateProgressbar(View view, Cursor cursor) {
ProgressBar progressBar = (ProgressBar) view
.findViewById(R.id.progressbarHorizontal);
progressBar.setProgressDrawable(view.getResources().getDrawable(
R.drawable.greenprogress));
progressBar.setMax(cursor.getInt(cursor.getColumnIndex("numberpages")));
progressBar.setProgress(cursor.getInt(cursor
.getColumnIndex("currentpage"))); …Run Code Online (Sandbox Code Playgroud)