java - 在 Android 应用程序中使用 XML 更改进度条颜色

标签 java android eclipse colors progress-bar

我正在尝试更改水平进度条(前景)的颜色。我遇到了this example并试图根据它对我的 XML 文件进行建模。但是,我在以下语句中遇到编译器错误:

myProgressBar.setProgressDrawable(R.drawable.progress_horizontal);

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

我认为原因在 R.java 文件中,我看到以下行:

public static final int progress_horizontal=0x7f02002f;

那么,我是将这个 XML 文件定义为可绘制对象而不是整数,还是有其他方法可以解决这个问题?

谢谢。

编辑:包括 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中内部类的所有字段通过android:id xml属性引用res文件夹下的资源。 Resouce类可以通过使用资源 id 来检索资源对象。所以,你可以通过Resource.getDrawable(int)得到Drawable对象。 .

关于java - 在 Android 应用程序中使用 XML 更改进度条颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3950956/

相关文章:

java - 使用 Maven 和 NetBeans 在 Tomcat 上进行增量热部署

Android 单选按钮图片

android - Kotlin 泛型抽象类

android - 如何通过 Android 模拟器在 Windows 中使用 telnet?

Mac OX 上的 Android Eclipse xml 编辑器自动完成功能无法正常工作

Java Reflection - 参数数量错误;期望 0,得到 1

Java:NoSuchMethodException,即使该方法存在

java - 启动tomcat时验证eclipse中的jar文件错误

java - 用于列出项目中所有类的 Eclipse 键盘快捷键

java - Java中按值顺序遍历二叉树