如何在Android上更改默认的ProgressBar圆形颜色

16

我目前在我的Android项目中使用一个通过gradle导入的外部库。
这个库会显示一个带有ProgressBar圆圈的通知栏。 以下是我在其源代码中发现的代码:

<ProgressBar
            android:id="@+id/progress_bar"
            android:layout_height="match_parent"
            android:layout_marginBottom="4dp"
            android:layout_marginTop="4dp"
            style="@style/SuperActivityToast_Progress_ProgressBar"/>

相关风格是这个:

<style name="SuperActivityToast_Progress_ProgressBar" parent="android:Widget.Holo.ProgressBar">
    <item name="android:layout_width">32dp</item>
    <item name="android:layout_marginLeft">8dp</item>
</style>
如果我理解正确,所显示的圆圈的颜色是从默认的颜色(在我的手机上是绿色)派生出来的。我需要改变它!
现在,我不能修改源代码,并且库本身也不提供以编程方式设置样式的可能性。
有没有一种方法可以在应用程序级别更改默认样式或更好地覆盖此特定样式?
谢谢 Davide

可能是Android更改水平进度条颜色的重复问题。 - wbk727
4个回答

62
如果您正在使用AppCompat主题,则它使用accentColor来对圆形进行着色。
如果您想将其着色为与主题不同的颜色,则应考虑使用ThemeOverlay。例如,如果您想使圆形着色为红色,则可以执行以下操作:
在您的styles.xml中:
<style name="RedAccent" parent="ThemeOverlay.AppCompat.Light">
    <item name="colorAccent">#F00</item>
</style>

在您的ProgressBar中,将主题设置为RedAccent

<ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:theme="@style/RedAccent"/>

你的圆现在将会是红色!


2
谢谢!快速且简单的解决方案,但是只有在包含“@style”引用主题后才能生效,例如android:theme="@style/RedAccent" - 谢谢 - Clive Sargeant
是的,@CliveSargeant,发现得好。由于我们引用了样式,所以需要使用“@style/”。我已更新答案。 - bond
这是一个非常有用的方法。我之前没有意识到我们可以以这种方式覆盖应用程序样式! - Mapsy
你是传奇的定义。 - Hudi Ilfeld
这个解决方案对我不起作用,我不得不按照下面其中一个答案所述设置colorControlActivated - yuval
与所有其他解决方案相比,这是一个伟大的答案。 - WHOATEMYNOODLES

30

经过多次尝试,我找到了一个解决方案:

ProgressBar progBar = (ProgressBar) context.getActivity().findViewById(R.id.progress_bar);
if (progBar != null) {
    progBar.setVisibility(View.VISIBLE);
    progBar.setIndeterminate(true);
    progBar.getIndeterminateDrawable().setColorFilter(0xFFFFFFFF, android.graphics.PorterDuff.Mode.MULTIPLY);
}

简单来说,我会获取由该库创建的进度条对象的引用,然后更改其属性。(在我的活动中,我必须在“OnStart”方法中执行此操作,否则它将为空) 最重要的部分是“setColorFilter”,它起到了魔法般的作用。


1
Mode.MULTIPLY会将颜色与进度条颜色相乘,如果想要纯实心颜色,则使用PorterDuff.Mode.SRC_ATOP。 - Ramesh Kumar

12

以备将来参考,这是我做出的更改:

在你的values/styles.xml文件中更改AppTheme中的colorControlActivated:

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Main theme colors -->
    ....
    <!-- Color for circle in progress bar -->
    <item name="colorControlActivated">#DC0808</item>
</style>

使用这种方法,您无需在xml文件中对<ProgressBar/>标签执行任何操作。


colorControlActivated 会影响其他东西吗?如果有,是哪些?在暗色主题下它的行为是否不同? - android developer
@VivekPratapSingh 你的清单文件中是否设置了AppTheme?
<application android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher" android:supportsRtl="true" android:theme="@style/AppTheme">
- Heinous Games

8
只需像下面这样为 ProgressBar 添加颜色:
    <ProgressBar
    android:id="@+id/progressbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:visibility="gone"
    android:indeterminateTint="@color/colorPrimary"  // add color here
    android:layout_centerVertical="true"/>

请注意,indeterminateTint 仅适用于 API 级别 21 及更高版本! - Bruno Bieri
stayed mass like the pest - luke cross

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接