[英]change the color of a ProgressBar based on a variable
我正在制作一個帶有一些 ProgressBars 的 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>
進度顏色設置為白色作為背景。 我怎么能改變它?
在 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));
如果您收到錯誤下划線 - “預期的字符串類型資源”只需添加注釋
@SuppressLint("ResourceType")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.