【问题标题】:Changing progressbar color causes progressbar to not display更改进度条颜色会导致进度条不显示
【发布时间】:2011-10-21 04:54:35
【问题描述】:

搜索该站点后,我发现以下代码可以更改进度条的颜色。这在我第一次调用代码时工作正常(进度条变为绿色),但是每次调用代码后我都会得到一个空白的进度条。有没有人遇到过这个问题?如果是这样,每次调用“setProgressDrawable”时能够更改进度条颜色的解决方案是什么?

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

在 strings.xml 文件中定义颜色

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

改变颜色的代码

 Rect bounds = bar.getProgressDrawable().getBounds();
 bar.setProgressDrawable(getResources().getDrawable(R.drawable.green_progress));
 bar.getProgressDrawable().setBounds(bounds);

【问题讨论】:

    标签: android android-layout progress-bar


    【解决方案1】:

    我终于找到了解决问题的方法。如果我要更改的颜色与已设置的颜色相同,进度条将变为空白。因此,第一次使用上面的代码时,进度条将从默认的黄色变为绿色。但是,下次调用代码时,进度条(已经设置为绿色)再次设置为绿色,结果整个进度条将变为空白。

    为了解决这个问题,我记录了进度条当前显示的颜色,当我再次设置颜色时,我只设置它是否与当前显示的颜色不同。

    希望这可以帮助其他发现自己处于相同情况的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-02
      • 2014-03-27
      • 2014-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多