我正在尝试使用Java动态更改自定义进度条的颜色。
这是主布局上ProgressBar的实现:
[activity_main.xml]
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:progressDrawable="@drawable/custom_progress"
android:background="@drawable/custom_progress_bar"
android:max="100"
android:progress="80" />
我创建了两个单独的可绘制对象来自定义进度条,如下所示:
[自定义进度条.xml 和自定义进度.xml]
<!-- custom_progress_bar.xml -->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<corners android:radius="8dp"/>
<solid android:color="@color/white"/>
</shape>
</item>
</layer-list>
<!-- custom_progress.xml -->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/progress_solid">
<clip>
<shape android:shape="rectangle">
<corners android:radius="8dp"/>
<solid android:color="@color/progress"/>
</shape>
</clip>
</item>
</layer-list>
这是应该将进度条从绿色(@color/progress)更改为红色的Java代码:
[MainActivity.java]
LayerDrawable layerDrawable = (LayerDrawable) ContextCompat.getDrawable(this, R.drawable.custom_progress);
ClipDrawable progressSolid = (ClipDrawable) layerDrawable.findDrawableByLayerId(R.id.progress_solid);
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(Color.RED);
progressSolid.setDrawable(gradientDrawable);
progressBar.invalidate();
这就是主题,(我将两个主题设置为完全相同):
[主题.xml 和主题.xml(夜间)]
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.CountdownCalender" parent="Theme.Material3.DayNight.NoActionBar">
<!-- Customizing the light theme here. -->
<item name="colorPrimaryVariant">@color/black</item>
<item name="colorPrimaryDark">@color/black</item>
<item name="colorPrimary">@color/progress</item>
<item name="colorOnPrimary">@color/white</item>
<item name="colorSurface">@color/white</item>
</style>
<style name="Theme.CountdownCalender" parent="Base.Theme.CountdownCalender" />
<style name="CalenderViewDateCustomText">
<item name="colorControlActivated">@drawable/selected_day</item>
<item name="android:textColor">@color/white</item>
<item name="android:weekDayTextAppearance">@color/accent</item>
</style>
</resources>
结果:
这不是我想要的结果。我希望它变成红色,如 Java 代码所示。我缺少什么?我的副驾驶聊天机器人没有帮助..我非常感谢您的帮助。预先感谢您。
解决方案:原来我只需要这几行:
LayerDrawable layerDrawable = (LayerDrawable) progressBar.getProgressDrawable();
Drawable progressFill = layerDrawable.getDrawable(1);
progressFill.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
并替换之前的代码。我还没有研究最后一行到底是什么,但它有效。
假设
progress
是一个保存进度条引用的变量,那么您可以使用以下 Java 代码以编程方式更改进度颜色:
LayerDrawable drawable = (LayerDrawable) progress.getProgressDrawable();
Drawable progressLayer = drawable.findDrawableByLayerId(android.R.id.progress);
setTintList(progressLayer.mutate(), context.getResources().getColorStateList(R.color.white, context.getTheme()))
Kotlin 中的等效项:
val drawable = progress.progressDrawable as LayerDrawable
val progressLayer = drawable.findDrawableByLayerId(android.R.id.progress)
setTintList(progressLayer.mutate(), resources.getColorStateList(R.color.white, theme))
setTintLint
是来自 DrawableCompat
的静态函数。颜色应该是有效的 ColorRes 资源。
希望这有帮助。
progressBar.progressTintList = ContextCompat.getColorStateList(this, R.color.teal_700)