【发布时间】:2021-11-06 12:49:54
【问题描述】:
我需要以编程方式更改水平进度条的颜色。
我需要用color 设置进度条,用backgroundColor 设置背景
所以我需要为我的进度条设置一个新的progressDrawable或更改现有的。
进度条xml:
<ProgressBar
android:id="@+id/pb_stat_value"
style="@style/CustomProgressBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginEnd="@dimen/margin_prog_bar_medium"
android:layout_weight="5" />
CustomProgressBar 风格:
<style name="CustomProgressBar" parent="android:Widget.ProgressBar.Horizontal">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">@drawable/custom_progress_bar_horizontal</item>
<item name="android:minHeight">@dimen/height_progress_bar_medium</item>
<item name="android:maxHeight">@dimen/height_progress_bar_large</item>
</style>
我需要引用的custom_pogress_bar_horizzontal.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape android:shape="rectangle" >
<solid android:color="@color/white" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="@color/black" />
</shape>
</clip>
</item>
</layer-list>
我正在尝试使用此代码,但我只得到背景:
val shape = GradientDrawable()
shape.shape = GradientDrawable.RECTANGLE
shape.setColor(backgroundColor)
pbStatValue.progressDrawable = shape
【问题讨论】:
标签: android xml android-drawable android-progressbar