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

Dav*_*ide 14 geometry android styles overriding android-progressbar

我目前在通过gradle导入的Android项目中使用外部库.
此库显示带有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"/>
Run Code Online (Sandbox Code Playgroud)

相关的风格是这样的:

<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>
Run Code Online (Sandbox Code Playgroud)

如果我理解相关信息,则显示的圆圈颜色来自默认颜色(手机上的绿色).我需要改变它!

现在,我无法修改源代码,库本身也不能让我以编程方式设置样式.

有一种方法可以在应用级别更改默认样式或更好地覆盖此特定样式?

谢谢戴维德

bon*_*ond 51

如果您使用的是AppCompat主题,则使用accentColor为圆圈着色.

如果您想将其着色为与主题不同的颜色,则应考虑使用ThemeOverylay.例如,如果要制作圆形色调red,可以执行以下操作:

在你的 styles.xml

<style name="RedAccent" parent="ThemeOverlay.AppCompat.Light">
    <item name="colorAccent">#F00</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在你的ProgressBar,设置主题RedAccent.

<ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:theme="@style/RedAccent"/>
Run Code Online (Sandbox Code Playgroud)

而你的圈子现在将呈红色!

  • 谢谢!快速简单的解决方案,只有在通过包含"@style"引用主题后才能工作,所以android:theme ="@ style/RedAccent" - 谢谢 (2认同)

Dav*_*ide 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);
}
Run Code Online (Sandbox Code Playgroud)

简单地说,我将获得库创建的进度条对象的引用,并更改它的属性.(在我的活动中,我必须在"OnStart"方法中执行此操作,否则它为null)最重要的部分是执行魔术的"setColorFilter".


小智 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"/>
Run Code Online (Sandbox Code Playgroud)


Red*_*d M 6

供将来参考,此更改对我有用:

values / styles.xml文件中更改colorControlActivated内部:AppTheme

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

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