我正在制作带有一些 ProgressBar 的 Android 应用。 ProgressBar 背景必须始终为白色,而进度颜色必须根据变量而变化,例如:
这是ProgressBar样式的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="ring"
android:thicknessRatio="16"
android:useLevel="false">
<solid android:color="#FFFF"/>
</shape>
</item>
<item android:id="@android:id/progress">
<shape
android:shape="ring"
android:thicknessRatio="16"
android:useLevel="true">
<solid android:color="@color/white" />
</shape>
</item>
</layer-list>
进度颜色设置为白色作为背景。怎么改?
答案 0 :(得分:0)
在 xml 中设置您的进度条:
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateDrawable="@drawable/nameofyourcustomprogressbar"
当您访问微调器时,您可以设置进度颜色属性:
ProgressBar spinner = (ProgressBar) findViewById(R.id.progressBar1);
spinner.getIndeterminateDrawable().setColorFilter(Color.parseColor("#bb86fc"), PorterDuff.Mode.MULTIPLY);
#bb86fc 是十六进制字符串格式的颜色。
如果您在 values/colors.xml 中列出了一种颜色,并希望通过这种方式访问一种颜色:
<resources>
<color name="purple_200">#FFBB86FC</color>
</resources>
从 values/colors.xml 访问您想要的颜色:
Color.parseColor(getString(R.color.purple_200));
如果你得到一个错误下划线 - "expected resource of type string" 只需添加注释
@SuppressLint("ResourceType")