如何在android中使用材料设计制作多色不确定线性进度指示器?
我想实现一个类似于这样的进度条:
在材料的设计文档,它说我需要设置indeterminateAnimationType来contiguous实现这一目标,并提供三种颜色。但是当indicatorColor属性只接受一种颜色时如何提供三种颜色?
当我运行这段代码时,它抛出一个异常说Contiguous indeterminate animation must be used with 3 or more indicator colors:
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
app:indeterminateAnimationType="contiguous"
app:hideAnimationBehavior="outward"
app:showAnimationBehavior="inward"
app:trackThickness="5dp" />
回答
使用连续动画至少需要 3 种指示器颜色。
只需将indicatorColor属性与数组一起使用:
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/progress"
android:indeterminate="true"
app:indeterminateAnimationType="contiguous"
app:indicatorColor="@array/progress_colors"
../>
与array/progress_colors:
<integer-array name="progress_colors">
<item>@color/....</item>
<item>@color/....</item>
<item>@color/....</item>
</integer-array>