我有一个使用ProgressBar类的进度条。
只需要这样做:
progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
我需要根据输入值更改那个的颜色,像这样:
int color = "red in RGB value".progressBar.setColor(color)
因为进度条是可定制的,所以我不能使用XML布局。
我有一个使用ProgressBar类的进度条。
只需要这样做:
progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
我需要根据输入值更改那个的颜色,像这样:
int color = "red in RGB value".progressBar.setColor(color)
因为进度条是可定制的,所以我不能使用XML布局。
布局 = activity_main.xml:
<ProgressBar
android:id="@+id/circle_progress_bar_middle"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:max="100"
android:rotation="-90"
android:indeterminate="false"
android:progressDrawable="@drawable/my_drawable_settings2" />
ProgressBar myProgressBar = (ProgressBar) view.findViewById(R.id.circle_progress_bar_middle);
myProgressBar.setProgressDrawable(getResources().getDrawable(R.my_drawable_settings1));
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/progress">
<shape
android:innerRadius="55dp"
android:shape="ring"
android:thickness="9dp"
android:useLevel="true">
<gradient
android:startColor="#3594d1"
android:endColor="@color/white"
android:type="sweep" />
</shape>
</item>
</layer-list>
setColorFilter
函数的两个参数已经被弃用,而且其他建议使用LightingColorFilter
的方法对我也没有起作用。
val progressBar = ProgressBar(context, null, android.R.attr.progressBarStyle).apply {
val colorFilter = PorterDuffColorFilter(
ContextCompat.getColor(context, R.color.yourColor),
PorterDuff.Mode.MULTIPLY
)
indeterminateDrawable.colorFilter = colorFilter
}
这将以编程方式为您提供带有您的颜色的圆形进度条
试试这个:
progress_wheel.getIndeterminateDrawable().setColorFilter(Color.parseColor(getPreferences().getString(Constant.SECOND_COLOR, Constant.SECONDARY_COLOR)), android.graphics.PorterDuff.Mode.MULTIPLY);
CircularProgressIndicator circularProgressIndicator = findViewById(R.id.tenant_progress_color);
circularProgressIndicator.setIndicatorColor(color);
circularProgressIndicator.setTrackColor(R.color.gray);