使用 ProgressBar.setProgressDrawable 的颜色问题
我将进度条附加到列表的元素中,并动态更新该进度条。 但我面临的奇怪情况是......
当我的列表中有多个元素并且我使用 Progressbar.setProgressDrwable() 更改进度条的颜色时,列表中的所有进度条都会更新并且它们的颜色会根据需要改变。
但是,当我列表中只有一个元素时,此方法不起作用,我只能看到进度条的背景而不是颜色。
我还为进度条的背景和颜色创建了 XML 文件,但它也不起作用。
我的 ProgressDrawable 的 XMl 代码是
FirstColor.xml
<item android:id="@android:id/background">
<shape>
<corners android:radius="15dip" />
<gradient android:startColor="#A9A9A9" android:centerColor="#A9A9A9"
android:endColor="#A9A9A9" android:angle="90" />
<stroke android:width="5dp" android:color="#A9A9A9" />
</shape>
</item>
<item android:id="@android:id/progress">
<scale android:scaleGravity="left" android:scaleWidth="100%" >
<shape>
<corners android:radius="15dip" />
<gradient android:startColor="#ADFF2F" android:centerColor="#ADFF2F"
android:endColor="#ADFF2F" android:angle="90" />
<stroke android:width="5dp" android:color="#ADFF2F" />
</shape>
</scale>
</item>
SecondColor.xml
<item android:id="@android:id/background">
<shape>
<corners android:radius="15dip" />
<gradient android:startColor="#A9A9A9" android:centerColor="#A9A9A9"
android:endColor="#A9A9A9" android:angle="90" />
<stroke android:width="5dp" android:color="#A9A9A9" />
</shape>
</item>
<item android:id="@android:id/progress">
<scale android:scaleGravity="left" android:scaleWidth="100%" >
<shape>
<corners android:radius="15dip" />
<gradient android:startColor="#FFBF00" android:centerColor="#FFBF00"
android:endColor="#FFBF00" android:angle="90" />
<stroke android:width="5dp" android:color="#FFBF00" />
</shape>
</scale>
</item>
我用来更改进度条颜色的java代码是
progressbar1.setProgressDrawable(getResources().getDrawable(R.drawable.FirstColor)); Progressbar1.setProgress(amntPercentage.intValue());
Progressbar1.setProgressDrawable(getResources().getDrawable(R.drawable.SecondColor)); Progressbar1.setProgress(amntPercentage.intValue());
如果有人可以解决我的问题,那将是一个很大的帮助。 提前致谢..
I am appending progressbar in elements of a list and also dynamically updating that progressbar.
But the odd situation that i m facing is that..
When i have more than one element in my list and i change the color of progressbar by using Progressbar.setProgressDrwable(),
all progressbars in the list are updated and their color is changed as desired.
But when I have only one single element in the list this method doesn't work, All I can see the background of my progressbar not the color.
I have also created my XML file for progressbar's background and color but than also its not working.
My XMl code for ProgressDrawable is
FirstColor.xml
<item android:id="@android:id/background">
<shape>
<corners android:radius="15dip" />
<gradient android:startColor="#A9A9A9" android:centerColor="#A9A9A9"
android:endColor="#A9A9A9" android:angle="90" />
<stroke android:width="5dp" android:color="#A9A9A9" />
</shape>
</item>
<item android:id="@android:id/progress">
<scale android:scaleGravity="left" android:scaleWidth="100%" >
<shape>
<corners android:radius="15dip" />
<gradient android:startColor="#ADFF2F" android:centerColor="#ADFF2F"
android:endColor="#ADFF2F" android:angle="90" />
<stroke android:width="5dp" android:color="#ADFF2F" />
</shape>
</scale>
</item>
SecondColor.xml
<item android:id="@android:id/background">
<shape>
<corners android:radius="15dip" />
<gradient android:startColor="#A9A9A9" android:centerColor="#A9A9A9"
android:endColor="#A9A9A9" android:angle="90" />
<stroke android:width="5dp" android:color="#A9A9A9" />
</shape>
</item>
<item android:id="@android:id/progress">
<scale android:scaleGravity="left" android:scaleWidth="100%" >
<shape>
<corners android:radius="15dip" />
<gradient android:startColor="#FFBF00" android:centerColor="#FFBF00"
android:endColor="#FFBF00" android:angle="90" />
<stroke android:width="5dp" android:color="#FFBF00" />
</shape>
</scale>
</item>
and the java code that i m using to change the color of progressbar is
progressbar1.setProgressDrawable(getResources().getDrawable(R.drawable.FirstColor));
progressbar1.setProgress(amntPercentage.intValue());
progressbar1.setProgressDrawable(getResources().getDrawable(R.drawable.SecondColor));
progressbar1.setProgress(amntPercentage.intValue());
If anyone out there can solve my problem it would be a great help..
Thanks in advance..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在设置新的
progressDrawable
和设置progressvalue
之前,请确保您尚未将progressvalue
设置为“0”。发生这种情况是因为在进度条的
style.xml
中,我们仅为开始、中间和结束定义了 xml 字段;不适合“0”。在更改可绘制对象之前,我将进度值设置为“1”,并且我的项目工作正常。
Before setting the new
progressDrawable
and setting theprogressvalue
, make sure that you have not setted theprogressvalue
to '0'.This happens because in
style.xml
for progressbar we define the xml fields only for start, middle and end; not for '0'.I setted the progressvalue to '1' before changing the drawable and my project is working fine.