在Android应用程序中使用XML更改进度条颜色 [英] Changing progress bar color using XML in Android app

查看:95
本文介绍了在Android应用程序中使用XML更改进度条颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改水平进度条(前景)的颜色。我遇到了这个例子,我试图建模我的XML文件关闭它。但是,我在以下语句中收到编译器错误:

I'm trying to change the color of a horizontal progress bar (foreground). I came across this example and am trying to model my XML file off it. However, I get a compiler error at the following statement:

myProgressBar.setProgressDrawable(R.drawable.progress_horizontal);

错误是ProgressBar类型中的setProgressDrawable(Drawable)方法不适用于参数int)。

The error is "The method setProgressDrawable(Drawable) in the type ProgressBar is not applicable for the arguments (int)."

我相信原因是在R.java文件里面我看到如下一行:

I believe the reason is inside the R.java file I see the following line:

public static final int progress_horizontal=0x7f02002f;

那么,我将这个XML文件定义为drawable而不是整数,还是有另一种方式解决这个问题?

So, do I define this XML file as a drawable and not an integer, or is there another way to solve this?

谢谢。

编辑:包含XML文件

<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="#ffffd300"
                    android:centerColor="#ffffb600"
                    android:centerY="0.75"
                    android:endColor="#ffffcb00"
                    android:angle="270"
            />
        </shape>
    </clip>
</item>

推荐答案

R类由Android编译器生成,R.java中的内部类的所有字段都指向 res 文件夹中的资源 android:id xml属性。 Resouce class可以通过使用资源id来检索资源对象。因此,您可以通过 <$ c $获取Drawable对象c> Resource.getDrawable(int)

The R class is generated by Android compiler, all fields of the inner class in R.java refer to the resources in res folder by android:id xml attribute. The Resouce class can retrieve the resource object by using the resource id. So, you can get Drawable object through Resource.getDrawable(int).

这篇关于在Android应用程序中使用XML更改进度条颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆