gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:49:22 24 4
gpt4 key购买 nike

我正在尝试更改水平进度条(前景)的颜色。我遇到了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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com